Found 1359 Articles for MongoDB

MongoDB query for counting the distinct values across all documents?

AmitDiwan
Updated on 14-May-2020 10:22:06

272 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo718.insertOne( ...    { ...       "id":101, ...       "details": ...       { ...          "OtherDetails": ["Chris", "Mike", "Sam"], "GroupName": ["Group-1"], "Info": [] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae25843417811278f5880") } > db.demo718.insertOne( ...    { ...       "id":102, ...       "details": ...       { ...          "OtherDetails": ["Chris", "David"], "GroupName": ["Group-1"], "Info": [] ...   ... Read More

Aggregation: group date in nested documents (nested object) and display the count?

AmitDiwan
Updated on 14-May-2020 10:20:20

204 Views

For aggregation, use aggregate() in MongoDB. Group the dates with $group. Let us create a collection with documents −> db.demo717.insertOne( ...    { ...       "shippingdetails": ...       [ ...          { ...             duedate:"2020-04-29 22:33:04", ...          }, ...          { ...             duedate:"2020-03-29 22:33:04", ...          }, ...          { ...             duedate:"2020-04-29 22:33:04", ...          }, ...       ... Read More

Creating hierarchical JSON in MongoDB?

AmitDiwan
Updated on 14-May-2020 10:16:43

162 Views

Use the following syntax to create hierarchical JSON in MongoDB −db.demo716.insertOne(    {       yourFieldName1,       yourFieldName2,       .       .       N,       "fieldName": {          yourFieldName1,          yourFieldName2,          .          .          N,          "fieldname":          [             {                yourFieldName1,                yourFieldName2,         ... Read More

Removing an object in a child collection in MongoDB?

AmitDiwan
Updated on 14-May-2020 10:12:12

439 Views

To remove an object in a child collection, use $pull in MongoDB. Let us create a collection with documents −> db.demo715.insertOne({ ...    _id:1, ...    details : ...    [ ...       { 'id' : '101', ...       'Information' : [ ...          { ...             'studentid' : '102', ...             "Name":"Bob" ...          }, ...          { ...             'studentid' : '103', ...             "Name":"Chris" ... Read More

MongoDB compound conditions to fetch documents?

AmitDiwan
Updated on 14-May-2020 10:10:06

75 Views

Let us create a collection with documents −> db.demo714.insertOne({FirstName:"Chris", LastName:"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam", LastName:"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David", LastName:"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John", LastName:"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }Display all documents from a collection with the help of find() method −> db.demo714.find();This will produce the following output −{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { ... Read More

Update tag records in MongoDB quickly

AmitDiwan
Updated on 14-May-2020 10:08:31

58 Views

Use $ along with update command to update tag records. Let us create a collection with documents −> db.demo713.insertOne( ...    { ...       tags: ...       [ ...          { ...             id:101, ...             Name:"Tag-1" ...          }, ...          { ...             id:102, ...             Name:"Tag-3" ...          }, ...          { ...           ... Read More

How to add a new field to all the documents in a MongoDB collection

AmitDiwan
Updated on 14-May-2020 10:06:37

388 Views

To add a new field, use $addFields in MongoDB. Let us create a collection with documents −> db.demo712.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f675d33e20ed1097b82") } > db.demo712.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6a5d33e20ed1097b83") } > db.demo712.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6e5d33e20ed1097b84") }Display all documents from a collection with the help of find() method −> db.demo712.find();This will produce the following output −{ "_id" : ObjectId("5ea85f675d33e20ed1097b82"), "Name" : "John" } { "_id" : ObjectId("5ea85f6a5d33e20ed1097b83"), "Name" : "Chris" } { "_id" : ObjectId("5ea85f6e5d33e20ed1097b84"), "Name" : "Bob" }Following is the query to add a ... Read More

MongoDB query to get documents with multiple conditions set in $or?

AmitDiwan
Updated on 14-May-2020 10:03:43

1K+ Views

Let us create a collection with documents −> db.demo711.insertOne({Name:"John", "Marks":75, Age:21, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c215d33e20ed1097b7e") } > db.demo711.insertOne({Name:"Chris", "Marks":55, Age:22, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c2c5d33e20ed1097b7f") } > db.demo711.insertOne({Name:"Bob", "Marks":45, Age:20, status:"Inactive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c3e5d33e20ed1097b80") } > db.demo711.insertOne({Name:"David", "Marks":85, Age:23, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c775d33e20ed1097b81") }Display all documents from a collection with the help of find() method −> db.demo711.find();This will produce the following output −{ "_id" : ObjectId("5ea85c215d33e20ed1097b7e"), "Name" : "John", "Marks" : 75, "Age" : 21, "status" : ... Read More

Sort id and reverse the items with MongoDB

AmitDiwan
Updated on 14-May-2020 10:01:05

858 Views

The $natural returns the documents in natural order. To reverse the items, use $natural:-1. Let us create a collection with documents −> db.demo710.insertOne({id:101, Name:"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a855d33e20ed1097b7a") } > db.demo710.insertOne({id:102, Name:"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a8d5d33e20ed1097b7b") } > db.demo710.insertOne({id:103, Name:"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a935d33e20ed1097b7c") } > db.demo710.insertOne({id:104, Name:"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a9b5d33e20ed1097b7d") }Display all documents from a collection with the help of find() method −> db.demo710.find();This will produce the following output −{ "_id" : ObjectId("5ea83a855d33e20ed1097b7a"), "id" : 101, "Name" : ... Read More

MongoDB aggregation to get two documents with the least marks

AmitDiwan
Updated on 14-May-2020 09:58:33

94 Views

To get the sorted list of marks, use $sort. Use $limit: 2 to display only two such documents with least marks. Let us create a collection with documents −> db.demo709.insertOne({Name:"John", "Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839005d33e20ed1097b76") } > db.demo709.insertOne({Name:"Chris", "Marks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839075d33e20ed1097b77") } > db.demo709.insertOne({Name:"David", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839125d33e20ed1097b78") } > db.demo709.insertOne({Name:"Bob", "Marks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839295d33e20ed1097b79") }Display all documents from a collection with the help of find() method −> db.demo709.find();This will produce the following output −{ "_id" ... Read More

Advertisements