Found 1359 Articles for MongoDB

How to find a certain element in the MongoDB embedded document?

AmitDiwan
Updated on 30-Jun-2020 08:02:54

130 Views

To find a certain element, use $project in MongoDB. Let us create a collection with documents −> db.demo744.insertOne( ...    { ...       studentInformation: ...       [ ...          { ...             studentName:"Robert", ...             grade:"A" ...          }, ...          { ...             studentName:"Bob", ...             grade:"C" ...          }, ...          { ...             ... Read More

How can I extract entire documents based on how they compare with their whole collection?

AmitDiwan
Updated on 30-Jun-2020 08:01:12

60 Views

For this, use $$ROOT in MongoDB. Let us create a collection with documents −> db.demo743.insertOne({id:1, "ShippingDate":"2020-01-21", value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead893a57bb72a10bcf0680") } > db.demo743.insertOne({id:2, "ShippingDate":"2020-05-10", value:30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead893c57bb72a10bcf0681") } > db.demo743.insertOne({id:3, "ShippingDate":"2020-05-10", value:60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead894657bb72a10bcf0682") } > db.demo743.insertOne({id:1, "ShippingDate":"2020-05-11", value:75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead895657bb72a10bcf0683") }Display all documents from a collection with the help of find() method −> db.demo743.find();This will produce the following output −{ "_id" : ObjectId("5ead893a57bb72a10bcf0680"), "id" : 1, "ShippingDate" : "2020-01-21", "value" : 50 ... Read More

I can print out each element of an array by iterating through all values, but can't get a specific element in MongoDB

AmitDiwan
Updated on 30-Jun-2020 07:59:03

1K+ Views

To fetch a specific element, iterate with forEach(). Let us create a collection with documents −> db.demo742.insertOne({ "userDetails": [ { "userName":"Robert", "CountryName":"UK" }, { "userName":"David", "CountryName":"AUS" } ]} ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead790b57bb72a10bcf0677") }Display all documents from a collection with the help of find() method −> db.demo742.find().pretty();This will produce the following output −{    "_id" : ObjectId("5ead790b57bb72a10bcf0677"),    "userDetails" : [       {          "userName" : "Robert",          "CountryName" : "UK"       },       {          "userName" : "David",   ... Read More

Updating MongoDB collection for _id?

AmitDiwan
Updated on 30-Jun-2020 07:57:08

83 Views

To update for _id, use $set in MongoDB. Let us create a collection with documents −db.demo741.insertOne({SubjectName:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead718657bb72a10bcf0672") } > db.demo741.insertOne({SubjectName:"C"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead718957bb72a10bcf0673") } > db.demo741.insertOne({SubjectName:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead718e57bb72a10bcf0674") }Display all documents from a collection with the help of find() method −> db.demo741.find();This will produce the following output −{ "_id" : ObjectId("5ead718657bb72a10bcf0672"), "SubjectName" : "MySQL" } { "_id" : ObjectId("5ead718957bb72a10bcf0673"), "SubjectName" : "C" } { "_id" : ObjectId("5ead718e57bb72a10bcf0674"), "SubjectName" : "Java" }Following is the query to updating MongoDB for _id ... Read More

How to display only the keys from nested MongoDB documents?

AmitDiwan
Updated on 30-Jun-2020 07:55:23

417 Views

Let us create a collection with documents −> db.demo740.insertOne({ ...    "details": ...    [ ...       { ...          Name:"Chris", ...          Age:21, ...          CountryName:"US" ...       }, ...       { ...          Name:"Bob", ...          Age:20, ...          CountryName:"UK", ...          isMarried:true ...       } ...    ] ... }); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead700c57bb72a10bcf066d") }Display all documents from a collection with the help ... Read More

How to push value with for loop in MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:52:34

810 Views

To push value, use save() along with for loop. Let us create a collection with documents −> for(var v=1; v db.demo739.find();This will produce the following output −{ "_id" : ObjectId("5ead6e7857bb72a10bcf0666"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0667"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0668"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf0669"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf066a"), "Name" : "Chris", "SubjectName" : "MongoDB" } { "_id" : ObjectId("5ead6e7857bb72a10bcf066b"), "Name" : "Chris", "SubjectName" : "MongoDB" }

Work with $push in MongoDB

AmitDiwan
Updated on 30-Jun-2020 07:51:10

122 Views

Let us create a collection with documents −> db.demo738.insertOne({Subjects:["C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead696557bb72a10bcf0661") } > db.demo738.insertOne({Subjects:["MySQL", "PL/SQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead696657bb72a10bcf0662") }Display all documents from a collection with the help of find() method −> db.demo738.find();This will produce the following output −{ "_id" : ObjectId("5ead696557bb72a10bcf0661"), "Subjects" : [ "C", "C++" ] } { "_id" : ObjectId("5ead696657bb72a10bcf0662"), "Subjects" : [ "MySQL", "PL/SQL" ] }Following is the query to push −>db.demo738.update({_id:ObjectId("5ead696657bb72a10bcf0662")}, {$push:{"Subjects":"MongoDB"}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of ... Read More

How to order by timestamp (descending order) in MongoDB

AmitDiwan
Updated on 30-Jun-2020 07:49:10

2K+ Views

To order by timestamp, use sort() in MongoDB. Let us create a collection with documents −> db.demo737.insertOne({"timestamp" : new ISODate("2020-04-01" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682157bb72a10bcf065c") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-10-31" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682757bb72a10bcf065d") } > db.demo737.insertOne({"timestamp" : new ISODate("2020-05-02" )}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead682a57bb72a10bcf065e") }Display all documents from a collection with the help of find() method −> db.demo737.find();This will produce the following output −{ "_id" : ObjectId("5ead682157bb72a10bcf065c"), "timestamp" : ISODate("2020-04-01T00:00:00Z") } { "_id" : ObjectId("5ead682757bb72a10bcf065d"), "timestamp" : ISODate("2020-10-31T00:00:00Z") } { "_id" : ... Read More

Filter query on array of embedded document with MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:47:50

245 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo736.insertOne( ...    { ...       "_id": "101", ...       "details1": [ ...          { ...             "details2": [ ...                { ...                   "details3": { ...                      "Name": "John" ...                   } ...                } ... ... Read More

Using MongoDB updateOne() & insertOne()

AmitDiwan
Updated on 30-Jun-2020 07:44:44

660 Views

MongoDB insertOne() inserts a document into a collection, whereas updateOne() update a single document in a collection based on a query filter.Let us create a collection with documents −> db.demo735.insertOne({id:1, Name:"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51b657bb72a10bcf0652") } > db.demo735.insertOne({id:1, Name:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51bb57bb72a10bcf0653") } > db.demo735.insertOne({id:1, Name:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51be57bb72a10bcf0654") } > db.demo735.insertOne({id:1, Name:"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead51c757bb72a10bcf0655") }Display all documents from a collection with the help of find() method −> db.demo735.find();This will produce the following output −{ "_id" ... Read More

Advertisements