Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/474.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript Mongodb mongoose-如果字段不存在文档,如何将文档添加到数组中_Javascript_Node.js_Mongodb_Mongoose - Fatal编程技术网

Javascript Mongodb mongoose-如果字段不存在文档,如何将文档添加到数组中

Javascript Mongodb mongoose-如果字段不存在文档,如何将文档添加到数组中,javascript,node.js,mongodb,mongoose,Javascript,Node.js,Mongodb,Mongoose,像这样的文件 { "_id" : ObjectId("5db65eb2a2f3a61fe88e162a"), "devicesCxt" : [ { "deviceId" : "1232", "userAgent" : "PostmanRuntime/7.19.0", "online" : false, "_id" : ObjectId("5db65eb2a2f3

像这样的文件

{
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162a"), 
    "devicesCxt" : [
        {
            "deviceId" : "1232", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162c"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        }
    ], 
}
{
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162a"), 
    "devicesCxt" : [
        {
            "deviceId" : "1232", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162c"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        },
        {
            "deviceId" : "1233", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162b"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        }
    ],
}
我想补充一下

{
    "deviceId" : "1233", 
    "userAgent" : "PostmanRuntime/7.19.0", 
    "online" : false, 
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162b"), 
    "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
}
我想要这样的东西

{
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162a"), 
    "devicesCxt" : [
        {
            "deviceId" : "1232", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162c"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        }
    ], 
}
{
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162a"), 
    "devicesCxt" : [
        {
            "deviceId" : "1232", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162c"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        },
        {
            "deviceId" : "1233", 
            "userAgent" : "PostmanRuntime/7.19.0", 
            "online" : false, 
            "_id" : ObjectId("5db65eb2a2f3a61fe88e162b"), 
            "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
        }
    ],
}
如果
deviceId:1232
不允许,否则
deviceId:1233
可以成功

设备ID不能有相同的对象

deviceId
在数组中应保持唯一


我如何才能做到这一点?

已更新

您的模式可以是这样的:

const mySchema = new Schema({
  deviceCtx: [{
      deviceId : { type: String, unique: true},
      userAgent : { type: String, },
      online : { type: Boolean, },
      loginAt : { type: Date, },
    }]
});
要在数组中添加新项,请执行以下操作:

const mySchema = await mySchema.find({ _id: "5db65eb2a2f3a61fe88e162a" });

const newDeviceId = req.body.deviceId;
mySchema.devicesCxt.push({
  deviceId : newDeviceId,
  userAgent : "PostmanRuntime/7.19.0",
  online : false,
  loginAt : new Date()
});

await mySchema.save();
用这个

let deviceid = {
    "deviceId" : "1233", 
    "userAgent" : "PostmanRuntime/7.19.0", 
    "online" : false, 
    "_id" : ObjectId("5db65eb2a2f3a61fe88e162b"), 
    "loginAt" : ISODate("2019-10-28T03:21:22.178+0000")
}
myModel.update(

    { $push: { devicesCxt: deviceid } },
);

但是,如果您想向数组添加_id,您需要在模式中定义它

可以有一个条件更新查询,以防止在传入对象的
设备id已经存在时将文档添加到
数组字段中

Mongo查询:

const incomingDoc = {
  deviceId: "1233",
  userAgent: "PostmanRuntime/7.19.0",
  online: false,
  _id: ObjectId("5db65eb2a2f3a61fe88e162b"),
  loginAt: ISODate("2019-10-28T03:21:22.178+0000")
};

db.collection.update(
  {
    _id: idToFilterIfAny,
    "devicesCxt.deviceId": { $ne: incomingDoc.deviceId }
  },
  { $push: { devicesCxt: incomingDoc } }
);

您是否尝试过此示例谢谢Jayakumar Thangavel,这不是我想要的,我希望deviceId不要重复。您可以尝试在deviceId字段上创建一个。我正在尝试找到一种方法来确定当adding@Amorous如果我理解正确,您希望更新/添加新的传入对象到
devicesCxt
array如果它不包含具有相同
设备ID
的对象。。正确吗?@Amorous您想让您的
设备ID
自动递增吗?那么上面的代码应该可以工作。因为,deviceId设置为
unique
。让我再试一次,再次感谢@Dijkstra,不同的文档允许相同的deviceId存在,但同一数组中只有一个。谢谢你的回答。mongoose有办法在保存deviceId之前确定它是否存在吗?如果没有,我只能修改我的schema@Amorous检查这个:这个答案是@Amorous,看看这是否是你所需要的。基本思想是在一个字段上进行过滤以更新,该字段在
设备ID
不存在时添加一个文档。太好了,这正是我需要的,非常感谢@ambianBeing,我不想更改我的模式,这对我来说是最好的方法。再次感谢您原谅我,如果有执行修改,则会添加其他案例。我该怎么办?@Amorous建议在添加所有新案例的情况下提出一个新的独立问题,这有助于在SO获得更好的可视性和其他人的帮助,并将其链接指向我,我会查看它。