AmitDiwan has Published 10744 Articles

MongoDB query to update all documents matching specific IDs

AmitDiwan

AmitDiwan

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

823 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" ... Read More

MongoDB query to check the existence of multiple fields

AmitDiwan

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" : ... Read More

MongoDB function to return a specific data/value?

AmitDiwan

AmitDiwan

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

237 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" : ... Read More

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

AmitDiwan

AmitDiwan

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

273 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, ... Read More

How to remove primary key from MongoDB?

AmitDiwan

AmitDiwan

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

369 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,   ... Read More

Return all ages records that are ints with a MongoDB query

AmitDiwan

AmitDiwan

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

227 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" : ... Read More

Find documents which contain a particular value in MongoDB using Regular Expressions?

AmitDiwan

AmitDiwan

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

67 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" : ... Read More

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

AmitDiwan

AmitDiwan

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

300 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" : { ... Read More

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

AmitDiwan

AmitDiwan

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

740 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"} ... } ... ); ... Read More

Using MongoDB nested $group and $sum to get the count of stocks with similar ProductID?

AmitDiwan

AmitDiwan

Updated on 11-May-2020 09:35:34

642 Views

The $group in MongoDB is used to group input documents by the specified _id expression. Let us create a collection with documents −> db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 1 ... ... Read More

Advertisements