Using positional operator with hierarchies in MongoDB?

MongoDBDatabaseBig Data Analytics

Let us create a collection with documents −

> db.demo324.insertOne({"ListOfValues":[10,20,30]});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5e516349f8647eb59e562073")
}

Display all documents from a collection with the help of find() method −

> db.demo324.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e516349f8647eb59e562073"),
   "ListOfValues" : [
      10,
      20,
      30
   ]
}

Here is the query displaying how to use positional operator −

> db.demo324.update({"ListOfValues":[10,20,30]},{ $set: { "ListOfValues.$[]": "MongoDB" } }, { upsert: true } );
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Display all documents from a collection with the help of find() method −

> db.demo324.find().pretty();

This will produce the following output −

{
   "_id" : ObjectId("5e516349f8647eb59e562073"),
   "ListOfValues" : [
      "MongoDB",
      "MongoDB",
      "MongoDB"
   ]
}
raja
Published on 02-Apr-2020 06:26:03
Advertisements