Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Using positional operator with hierarchies in MongoDB?
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"
]
}Advertisements