Found 1349 Articles for MongoDB

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan
Updated on 31-Mar-2020 07:43:48

77 Views

For this, use double underscore( __) in find(). Let us first create a collection with documents −> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f3a625ddae1f53b621f2") }Following is the query to display all documents from a collection with the help of find() method −> db.nestedDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e06f39125ddae1f53b621f0"),    "Information" : {       "__StudentName" : "John Smith"    } } {    "_id" ... Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan
Updated on 31-Mar-2020 07:40:57

149 Views

For this, you can use aggregate(). Let us first create a collection with documents −> db.countOccurrencesDemo.insertOne({"ListOfValues":[65, 87, 89, 65, 67, 87, 87, 87]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102, 65, 87, 65, 89, 65, 89, 65, 89, 65]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06efaa25ddae1f53b621ec") }Following is the query to display all documents from a collection with the help of find() method −> db.countOccurrencesDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ef9325ddae1f53b621eb"), "ListOfValues" : [ 65, 87, 89, 65, 67, 87, 87, 87 ] } { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : ... Read More

Update only a single document in MongoDB

AmitDiwan
Updated on 31-Mar-2020 07:38:42

131 Views

To update only a single document in a collection. use updateOne(). Let us first create a collection with documents −> db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3725ddae1f53b621e8") } > db.updateOneDemo.insertOne({"StudentId":2, "StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3825ddae1f53b621e9") } > db.updateOneDemo.insertOne({"StudentId":1, "StudentFirstName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ed3825ddae1f53b621ea") }Following is the query to display all documents from a collection with the help of find() method −> db.updateOneDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ed3725ddae1f53b621e8"), "StudentId" : 1, "StudentFirstName" : "Chris" } { "_id" : ObjectId("5e06ed3825ddae1f53b621e9"), "StudentId" : 2, "StudentFirstName" ... Read More

Fetch specific field values in MongoDB

AmitDiwan
Updated on 31-Mar-2020 07:36:50

924 Views

To fetch specific field values, use $in operator. The $in selects the documents where the value of a field equals any value in the specified array.Let us first create a collection with documents −> db.indexesDemo.createIndex({"StudentFirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de4d25ddae1f53b621dd") } > db.indexesDemo.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de5825ddae1f53b621de") } > db.indexesDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06de6725ddae1f53b621df") } > db.indexesDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true, ... Read More

Appending an entry in one to many embedded documents with MongoDB

AmitDiwan
Updated on 30-Mar-2020 14:34:24

273 Views

To append an entry in MongoDB, use $push. Let us create a collection with documents −> db.demo253.insertOne( ...   { ...      _id: "101", ...      isActive: false, ...      details: [ ...         { ...            Name: "Chris", ...         }, ...         { ...            CountryName:"US" ...         } ...      ] ...   } ...); { "acknowledged" : true, "insertedId" : "101" }Display all documents from a collection with the help of find() ... Read More

Sort array in MongoDB query and project all fields?

AmitDiwan
Updated on 30-Mar-2020 14:32:20

579 Views

To sort array, use $sort. For projection, use $project in MongoBD aggregate(). Let us create a collection with documents −> db.demo252.insertOne( ...   {"Values" : [ { "v1" : 20, "v2" :30 }, { "v1" : 20, "v2" : 20 }, { "v1" : 10, "v2" : 7 } ] } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c2761627c0c63e7dba78") }Display all documents from a collection with the help of find() method −> db.demo252.find();This will produce the following output −{ "_id" : ObjectId("5e46c2761627c0c63e7dba78"), "Values" : [ { "v1" : 20, "v2" : 30 }, { "v1" : 20, ... Read More

Perform min/max with MongoDB aggregation

AmitDiwan
Updated on 30-Mar-2020 14:28:55

992 Views

For min/max in MongoDB, use $min and $max. Let us create a collection with documents −> db.demo251.insertOne({"Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0001627c0c63e7dba74") } > db.demo251.insertOne({"Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0031627c0c63e7dba75") } > db.demo251.insertOne({"Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c0061627c0c63e7dba76") } > db.demo251.insertOne({"Marks":76}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46c00c1627c0c63e7dba77") }Display all documents from a collection with the help of find() method −> db.demo251.find();This will produce the following output −{ "_id" : ObjectId("5e46c0001627c0c63e7dba74"), "Marks" : 78 } { "_id" : ObjectId("5e46c0031627c0c63e7dba75"), "Marks" : 87 } { "_id" : ... Read More

Find the MongoDB collection size for name “Chris”

AmitDiwan
Updated on 30-Mar-2020 14:20:41

63 Views

For this, use bsonsize() in MongoDB. Let us create a collection with documents −> db.demo250.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd501627c0c63e7dba70") } > db.demo250.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd531627c0c63e7dba71") } > db.demo250.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd561627c0c63e7dba72") } > db.demo250.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46bd5b1627c0c63e7dba73") }Display all documents from a collection with the help of find() method −> db.demo250.find();This will produce the following output −{ "_id" : ObjectId("5e46bd501627c0c63e7dba70"), "Name" : "Chris" } { "_id" : ObjectId("5e46bd531627c0c63e7dba71"), "Name" : "Bob" } { "_id" : ObjectId("5e46bd561627c0c63e7dba72"), "Name" ... Read More

Query BinData by Type in MongoDB

AmitDiwan
Updated on 30-Mar-2020 14:10:18

640 Views

To query by type, use subtype() in MongoDB. Let us create a collection with documents −> db.demo249.insertOne({ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") }); {    "acknowledged" : true,    "insertedId" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } > db.demo249.insertOne({"_id" : BinData(4, "CNDF66qIlCY92q1vFAAAAQ==")}); {    "acknowledged" : true,    "insertedId" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } > db.demo249.insertOne({"_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==")}); {    "acknowledged" : true,    "insertedId" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Display all documents from a collection with the help of find() method −> db.demo249.find();This will produce the following output −{ "_id" : BinData(0, "AQAAAAEBAAVlbl9VSwAAAAAAAAhv") } { "_id" : UUID("08d0c5eb-aa88-9426-3dda-ad6f14000001") } { "_id" : BinData(3, "CNDF66qJ29g92q1vFAAAEw==") }Following is ... Read More

MongoDB aggregate to convert multiple documents into single document with an array?

AmitDiwan
Updated on 30-Mar-2020 14:09:09

826 Views

For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo248.insertOne({"id":101, "Name":"Chris", "Age":21, "CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101, "Name":"Bob", "Age":22, "CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102, "Name":"Mike", "Age":20, "CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e46b6811627c0c63e7dba6f") }Display all documents from a collection with the help of find() method −> db.demo248.find();This will produce the following output −{ "_id" : ObjectId("5e46b6651627c0c63e7dba6d"), "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e46b6741627c0c63e7dba6e"), "id" : 101, ... Read More

Advertisements