简体   繁体   中英

Mongodb mongoose - How to add a document to an array if not present by a field

Document like this

{
    "_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")
        }
    ], 
}

I want to add this

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

I want something like this

{
    "_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")
        }
    ],
}

if deviceId: 1232 not allow,else deviceId: 1233 can succeed.

Can't have the same object for deviceId

deviceId should be kept unique in the array.

How can I do this?

UPDATED

Your schema could be something like this:

const mySchema = new Schema({
  deviceCtx: [{
      deviceId : { type: String, unique: true},
      userAgent : { type: String, },
      online : { type: Boolean, },
      loginAt : { type: Date, },
    }]
});

To add new item in the array:

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();

use this.

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 } },
);

but if you want to mongo add _id to your array you need to define it in your schema

Could have a conditional update query to prevent addition of document to devicesCxt array field when the deviceId of incoming object already present.$push

Mongo Query:

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 } }
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM