MongoDB Unwind to Get the Count

AmitDiwan
Updated on 11-May-2020 09:50:05

582 Views

The $unwind in MongoDB deconstructs an array field from the input documents to output a document for each element. Use $unwind along with aggregate() to get the count. Let us create a collection with documents −> db.demo478.insertOne( ... { ... ...    "Details" : { ...       _id:1, ...       "Information" : [ ...          { ...             "Name" : "Chris", ...             "Age":21 ...          }, ...          { ...             ... Read More

Get Real Document BSON Size in MongoDB

AmitDiwan
Updated on 11-May-2020 09:48:28

1K+ Views

You can use Object.bsonsize() to get real document size. It prints the BSON size of a document in bytes. Let us create a collection with documents −> db.demo477.insertOne({"ClientId":1, "ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82015fb0f3fa88e227908f") } > db.demo477.insertOne({"ClientId":2, "ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e820167b0f3fa88e2279090") } > db.demo477.insertOne({"ClientId":3, "ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e82016db0f3fa88e2279091") }Display all documents from a collection with the help of find() method −> db.demo477.find();This will produce the following output −{ "_id" : ObjectId("5e82015fb0f3fa88e227908f"), "ClientId" : 1, "ClientName" : "Chris" } { "_id" : ObjectId("5e820167b0f3fa88e2279090"), "ClientId" : 2, "ClientName" : ... Read More

MongoDB Query to Update All Documents Matching Specific IDs

AmitDiwan
Updated on 11-May-2020 09:46:17

829 Views

Use the updateMany() function to update all documents that match the filter criteria. Let us create a collection with documents −> db.demo476.insertOne({_id:1, "Name":"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo476.insertOne({_id:2, "Name":"David"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo476.insertOne({_id:3, "Name":"Bob"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo476.insertOne({_id:4, "Name":"Carol"}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo476.find();This will produce the following output −{ "_id" : 1, "Name" : "Chris" } { "_id" : 2, "Name" : "David" } { "_id" ... Read More

MongoDB Query to Check Existence of Multiple Fields

AmitDiwan
Updated on 11-May-2020 09:45:59

1K+ Views

To check the existence of multiple fields, use $exists along with $and. Let us create a collection with documents −> db.demo475.insertOne({"StudentFirstName":"Chris", "StudentAge":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c113b0f3fa88e2279088") } > db.demo475.insertOne({"StudentFirstName":"Bob", "StudentAge":21, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c127b0f3fa88e2279089") } > db.demo475.insertOne({"StudentFirstName":"David", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80c135b0f3fa88e227908a") }Display all documents from a collection with the help of find() method −> db.demo475.find();This will produce the following output −{ "_id" : ObjectId("5e80c113b0f3fa88e2279088"), "StudentFirstName" : "Chris", "StudentAge" : 23 } { "_id" : ObjectId("5e80c127b0f3fa88e2279089"), "StudentFirstName" : "Bob", "StudentAge" : 21, "StudentCountryName" : "US" } { ... Read More

MongoDB Function to Return a Specific Data Value

AmitDiwan
Updated on 11-May-2020 09:43:37

240 Views

To return a specific data, use findOne() in MongoDB. The findOne() method returns one document that satisfies the specified query criteria on the collection Let us create a collection with documents −> db.demo473.insertOne( ... { ...    "_id" : new ObjectId(), ...    "Name" : "Chris", ...    "details" : { ...       "X-Coordinate" :10, ...       "Y-Coordinate" :15 ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e805a07b0f3fa88e227907d") } > db.demo473.insertOne( ... { ...    "_id" : new ObjectId(), ...    "Name" : "Bob", ...    "details" : { ... Read More

Find Documents in MongoDB Where Field Equals Given Integer Value

AmitDiwan
Updated on 11-May-2020 09:43:08

279 Views

To find documents where a field is equal to given integer, use find(). Let us create a collection with documents −> db.demo472.insertOne({"Project_Id":-101, "ProjectName":"Online Customer Tracking"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80586cb0f3fa88e227907a") } > db.demo472.insertOne({"Project_Id":101, "ProjectName":"Online Banking System"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805884b0f3fa88e227907b") } > db.demo472.insertOne({"Project_Id":102, "ProjectName":"Online Library System"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805893b0f3fa88e227907c") }Display all documents from a collection with the help of find() method −> db.demo472.find();This will produce the following output −{ "_id" : ObjectId("5e80586cb0f3fa88e227907a"), "Project_Id" : -101, "ProjectName" : "Online Customer Tracking" } { "_id" : ObjectId("5e805884b0f3fa88e227907b"), "Project_Id" : 101, ... Read More

Remove Primary Key from MongoDB

AmitDiwan
Updated on 11-May-2020 09:41:06

380 Views

To remove primary key in MongoDB, set _id value to 0 i.e. set the field you want to exclude as 0 in find(). Let us create a collection with documents −> db.demo471.insertOne({"ClientId":101, "ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805711b0f3fa88e2279077") } > db.demo471.insertOne({"ClientId":102, "ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80571db0f3fa88e2279078") } > db.demo471.insertOne({"ClientId":103, "ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805724b0f3fa88e2279079") }Display all documents from a collection with the help of find() method −> db.demo471.find();This will produce the following output −{ "_id" : ObjectId("5e805711b0f3fa88e2279077"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e80571db0f3fa88e2279078"), "ClientId" ... Read More

Return All Ages Records That Are Integers with a MongoDB Query

AmitDiwan
Updated on 11-May-2020 09:40:45

230 Views

To get all the ages that are ints from records having string and int ages records, use $type. The $type in MongoDB $type selects documents where the value of the field is an instance of the specified BSON type.Let us create a collection with documents −> db.demo470.insertOne({"Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805456b0f3fa88e2279070") } > db.demo470.insertOne({"Age":"Unknown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80545cb0f3fa88e2279071") } > db.demo470.insertOne({"Age":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805461b0f3fa88e2279072") } > db.demo470.insertOne({"Age":"Not provided"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80546bb0f3fa88e2279073") }Display all documents from a collection with the help of find() ... Read More

Find Documents Containing a Value in MongoDB Using Regular Expressions

AmitDiwan
Updated on 11-May-2020 09:38:50

71 Views

To find documents containing a particular value with Regular Expressions, use MongoDB $regex. Let us create a collection with documents −> db.demo469.insertOne({"StudentName":"John Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80532fb0f3fa88e227906b") } > db.demo469.insertOne({"StudentName":"Chris Brown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80533ab0f3fa88e227906c") } > db.demo469.insertOne({"StudentName":"John Smith"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805341b0f3fa88e227906d") } > db.demo469.insertOne({"StudentName":"Jace Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805347b0f3fa88e227906e") } > db.demo469.insertOne({"StudentName":"David Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80534eb0f3fa88e227906f") }Display all documents from a collection with the help of find() method −> db.demo469.find();This will produce the following output −{ "_id" ... Read More

Find MongoDB Record That Is Two Levels Deep

AmitDiwan
Updated on 11-May-2020 09:38:28

309 Views

To find a MongoDB record that is two level deep, loop inside MongoDB $where. Let us create a collection with documents −> db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Chris", ...    "StudentAge" : 23 ... }, ... "SecondPosition" : { ...    "StudentName" : "David", ...    "StudentAge" : 20 ... } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e804e2fb0f3fa88e2279069") } > db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Carol", ...    "StudentAge" : 21 ... ... Read More

Advertisements