Found 1349 Articles for MongoDB

Get the real document BSON size in MongoDB?

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

805 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

675 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 the existence of multiple fields

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

824 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

158 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

Finding documents in MongoDB collection where a field is equal to given integer value?

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

213 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

How to remove primary key from MongoDB?

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

263 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 ints with a MongoDB query

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

149 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 which contain a particular value in MongoDB using Regular Expressions?

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

42 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

How do you find a MongoDB record that is two level deep?

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

223 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

How can I delete an item from an Object in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:35:56

613 Views

To delete an item from an object in MongoDB, use $unset. Let us create a collection with documents −> db.demo467.insertOne( ... { ... _id:101, ... "Information":{"Name":"Chris"} ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo467.insertOne( ... { ... _id:102, ... "Information":{"Name":"David"} ... } ... ); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo467.find();This will produce the following output −{ "_id" : 101, "Information" : { "Name" : "Chris" } } { "_id" : 102, "Information" : { "Name" : "David" } }Following ... Read More

Advertisements