4

I am new to MongoDB. I want to store User data in an array. The data is single unique document.lets say the document is like this.

User { id: number, name: string }
{
  _id : 001,
  room: User[],
}

I want to do something this...

  • push element (User) if room[] doesn't have it.
  • update User data.

Like there is upsert option in update operation.

  DB.Collection.updateOne(
      { _id: "001" },
      { $set: { name: "Bar" } },
      { upsert: true }
    );

How can I do something like this,In an Array of a Document?

2 Answers 2

5
+50

You can use $set operation to update Embedded Document with $(update) or $[$[<identifier>].But It's not work good with upsert So in order insert new Document use $push.Do something to update data or append data separately.

// push element (User) if room[] doesn't have it.

DB.Collection.updateOne(
    { "room.id": { $ne: "001" } },
    { $push: { "room": { id: "001", name: "Bar" } } },
);

// update User data

DB.Collection.updateOne(
    { "room.id": "001"  },
    { $set { "room.$": { id: "001", name: "Foo" } } },
);


Sign up to request clarification or add additional context in comments.

Comments

0
//you can use update with $set or $push based on whether you want to update one element in array with override/update or push another element.
> db.rooms.find();
{ "_id" : 1, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "raj" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "raj" ] }
> db.rooms.updateMany(   {},   {$set:{"room":["john"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john" ] }
> db.rooms.updateMany(   {},   {$push:{"room":["mike"]}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ] ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ] ] }
> db.rooms.updateMany(   {},   {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
>

the push will add a field in the document if it does not exist, so a specific check for field room is not required.

> db.rooms.find();
{ "_id" : 1, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "john", [ "mike" ], "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "john", [ "mike" ], "mike" ] }
> db.rooms.updateMany(
...   {},
...   {$unset:{"user":""}}
... );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 0 }
> db.rooms.updateMany(   {},   {$unset:{"room":""}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2 }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3 }
> db.rooms.updateMany(   {},   {$push:{"room":"mike"}} );
{ "acknowledged" : true, "matchedCount" : 3, "modifiedCount" : 3 }
> db.rooms.find();
{ "_id" : 1, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff51"), "id" : 2, "room" : [ "mike" ] }
{ "_id" : ObjectId("5f5e10f2598d922a1e6eff52"), "id" : 3, "room" : [ "mike" ] }
>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.