3

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?

Towkir
  • 3,889
  • 2
  • 22
  • 41
Amorous
  • 49
  • 6

3 Answers3

1

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();
ambianBeing
  • 3,449
  • 2
  • 14
  • 25
Shihab
  • 2,641
  • 3
  • 21
  • 29
1

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

Babak Abadkheir
  • 2,222
  • 1
  • 19
  • 46
  • Hi,babak abadkheir,Thanks for your answer.Does mongoose have a way to determine if deviceId exists before it can be saved? If not, I can only modify my schema – Amorous Oct 28 '19 at 14:10
  • @Amorous check this : https://www.npmjs.com/package/mongoose-unique-array and this answer https://stackoverflow.com/a/45574427/5677187 – Babak Abadkheir Oct 28 '19 at 15:14
1

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 } }
);
ambianBeing
  • 3,449
  • 2
  • 14
  • 25
  • @Amorous See if this is what you require. Where the basic idea is filter on a field to update which adds a document when the `deviceId` is not present. – ambianBeing Oct 28 '19 at 16:15
  • 1
    Great, this is exactly what I need,Thank you so much @ambianBeing,I don't want change my schema,This is the best way for me. Thanks again – Amorous Oct 28 '19 at 16:24
  • Excuse me, If there is an execution modification, other cases are added.How can i do? – Amorous Oct 29 '19 at 02:49
  • @Amorous Would suggest to ask a new self contained question with all the new cases added which helps to get better visibility and help from others at SO, and point me to the link of it, I ll check it out. – ambianBeing Oct 29 '19 at 03:09
  • https://stackoverflow.com/questions/58601090/mongoose-array-of-objects-if-there-is-an-execution-modification-other-cases-sh – Amorous Oct 29 '19 at 03:27