Found 1349 Articles for MongoDB

Cannot push into an array from MongoDB?

AmitDiwan
Updated on 06-Apr-2020 13:36:21

118 Views

To push into an array with MongoDB, use $push. Let us create a collection with documents −> db.demo445.insertOne({"ListOfFriends":["Robert", "Mike", "Sam", "Carol", "David", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78f099bbc41e36cc3caec2") }Display all documents from a collection with the help of find() method −> db.demo445.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e78f099bbc41e36cc3caec2"),    "ListOfFriends" : [       "Robert",       "Mike",       "Sam",       "Carol",       "David",       "Mike"    ] }Following is the query to push into an array −> db.demo445.update( ...    { ... Read More

How to improve MongoDB queries with multikey index in array?

AmitDiwan
Updated on 06-Apr-2020 13:32:46

124 Views

For this, use $elemMatch, which is used to query nested objects. Let us create a collection with documents −> db.demo444.insertOne( ...    { ...       "Information": [{ ...          id:1, ...          Name:"Chris" ...       }] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78ea87bbc41e36cc3caebf") } > db.demo444.insertOne( ...    { ...       "Information": [{ ...          id:2, ...          Name:"David" ...       }] ...    } ... ); {    "acknowledged" : true,   ... Read More

MongoDB profiler output: What is the “command” operation?

AmitDiwan
Updated on 06-Apr-2020 13:30:22

170 Views

The following operations are treated as command operation in MongoDB −1.count 2.findAndModify 3.aggregateFollowing is the example of count in MongoDB −Let us create a collection with documents −> db.demo443.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d281bbc41e36cc3caeb9") } > db.demo443.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d285bbc41e36cc3caeba") } > db.demo443.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78d288bbc41e36cc3caebb") }Display all documents from a collection with the help of find() method −> db.demo443.find();This will produce the following output −{ "_id" : ObjectId("5e78d281bbc41e36cc3caeb9"), "Name" : "Chris" } { "_id" : ObjectId("5e78d285bbc41e36cc3caeba"), "Name" : "Bob" } { "_id" : ... Read More

Getting distinct values from object array in MongoDB?

AmitDiwan
Updated on 06-Apr-2020 13:28:05

1K+ Views

To get distinct values from object array in MongoDB, use distinct(). Let us create a collection with documents −> db.demo442.insertOne( ...    { ... ...       "Information" : [ ...          { ...             "FirstName" : "John", ...             "Age" : 21 ...          }, ...          { ...             "FirstName" : "Sam", ...             "Age" : 23 ...          }, ...         ... Read More

MongoDB query to aggregate nested array

AmitDiwan
Updated on 06-Apr-2020 13:25:14

959 Views

To aggregate nested array in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo441.insertOne( ...    { ... ...       "Name" : "David", ...       "Age" : 21, ... ...       "details" : [ ...          { ...             "id" : 1, ...             "CountryName" : "US", ...             "details1" : [ ...                { ...                   "SubjectName" : ... Read More

How to count items in array with MongoDB?

AmitDiwan
Updated on 06-Apr-2020 13:17:15

212 Views

To count items in array, use length. Let us create a collection with documents −> db.demo440.insertOne( ...    { ...       "Name":"Chris", ...       "ListOfFriends":["John", "Sam", "Mike"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb5") } > > db.demo440.insertOne( ...    { ...       "Name":"David", ...       "ListOfFriends":["Mike", "Bob", "Carol"] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e78c63cbbc41e36cc3caeb6") }Display all documents from a collection with the help of find() method −> db.demo440.find();This will produce the following output −{ "_id" : ... Read More

Project field in MongoDB

AmitDiwan
Updated on 06-Apr-2020 13:14:17

158 Views

To project field in MongoDB, use $project. Let us create a collection with documents −> db.demo439.insertOne( ...    { ...       "Name" : "Chris", ...       "MarksInformation" : { ...          "Marks1" : 67, ...          "Marks2" :45, ...          "Marks3" : 78 ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e77833abbc41e36cc3caeab") } > db.demo439.insertOne( ...    { ...       "Name" : "David", ...       "MarksInformation" : { ...         ... Read More

How to remove duplicate record in MongoDB 3.x?

AmitDiwan
Updated on 06-Apr-2020 13:08:14

261 Views

To remove duplicate record, use aggregate(). Let us create a collection with documents −> db.demo438.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c37bbc41e36cc3caea1") } > db.demo438.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c3dbbc41e36cc3caea2") } > db.demo438.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c40bbc41e36cc3caea3") } > db.demo438.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c44bbc41e36cc3caea4") } > db.demo438.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e775c47bbc41e36cc3caea5") }Display all documents from a collection with the help of find() method −> db.demo438.find();This will produce the following output −{ "_id" : ObjectId("5e775c37bbc41e36cc3caea1"), "FirstName" : "Chris" } { ... Read More

Perform simple validation in MongoDB?

AmitDiwan
Updated on 06-Apr-2020 14:09:04

188 Views

For validation in MongoDB, use validator. Following is the query to create validation on collection in MongoDB −> db.createCollection( "demo437" , { ...    validator: { $jsonSchema: { ...       bsonType: "object", ...       required: [ "FirstName", "LastName"], ...       properties: { ...          FirstName: { ...             bsonType: "string", ...             description: "This is required" }, ...             LastName: { ...                bsonType: "string", ...         ... Read More

How to merge multiple documents in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 14:22:58

1K+ Views

To merge multiple documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo436.insertOne( ...    { ...       "_id" : "101", ...       "Name": "Chris", ...       "details" : [ ...          { ...             "CountryName" : "US", ...             "Age" : 21 ...          } ...       ], ...       "Price" : 50 ...    } ... ); { "acknowledged" : true, "insertedId" : "101" } > db.demo436.insertOne( ... ... Read More

Advertisements