Found 1354 Questions for MongoDB

Query an array of embedded documents in MongoDB and push another?

AmitDiwan
Updated on 15-May-2020 05:45:05
For this, use $push along with update. Let us create a collection with documents −> db.demo573.insertOne( ...    { ...       '_id' :101, ...       'SearchInformation' : [ ...          { ...             'Site' : 'Facebook.com', ...             'NumberOfHits' : 100 ...          }, ...          { ...             'Site' : 'Twitter.com', ...             'NumberOfHits' : 300 ...          } ...       ... Read More

MongoDB query with case insensitive search?

AmitDiwan
Updated on 15-May-2020 05:41:07
For case, insensitive search, use regex in find() method. Following is the syntax −db.demo572.find({"yourFieldName" : { '$regex':/^yourValue$/i}});To understand the above syntax, let us create a collection with documents −> db.demo572.insertOne({"CountryName":"US"});{    "acknowledged" : true, "insertedId" : ObjectId("5e915f0e581e9acd78b427f1") } > db.demo572.insertOne({"CountryName":"UK"});{    "acknowledged" : true, "insertedId" : ObjectId("5e915f17581e9acd78b427f2") } > db.demo572.insertOne({"CountryName":"Us"});{    "acknowledged" : true, "insertedId" : ObjectId("5e915f1b581e9acd78b427f3") } > db.demo572.insertOne({"CountryName":"AUS"});{    "acknowledged" : true, "insertedId" : ObjectId("5e915f20581e9acd78b427f4") } > db.demo572.insertOne({"CountryName":"us"});{    "acknowledged" : true, "insertedId" : ObjectId("5e915f25581e9acd78b427f5") }Display all documents from a collection with the help of find() method −> db.demo572.find();This will produce the following output −{ "_id" : ObjectId("5e915f0e581e9acd78b427f1"), ... Read More

Find value above a specific value in MongoDB documents?

AmitDiwan
Updated on 15-May-2020 05:39:06
To find values above a specific value, the following is the syntax using $gte in MongoDB −db.yourCollectionName.find({yourFieldName:{$gte:yourValue}});Let us create a collection with documents −> db.demo571.insertOne({"Price":140});{    "acknowledged" : true, "insertedId" : ObjectId("5e909b3439cfeaaf0b97b587") } > db.demo571.insertOne({"Price":100});{    "acknowledged" : true, "insertedId" : ObjectId("5e909b3639cfeaaf0b97b588") } > db.demo571.insertOne({"Price":110});{    "acknowledged" : true, "insertedId" : ObjectId("5e909b3839cfeaaf0b97b589") } > db.demo571.insertOne({"Price":240});{    "acknowledged" : true, "insertedId" : ObjectId("5e909b3c39cfeaaf0b97b58a") }Display all documents from a collection with the help of find() method −> db.demo571.find();This will produce the following output −{ "_id" : ObjectId("5e909b3439cfeaaf0b97b587"), "Price" : 140 } { "_id" : ObjectId("5e909b3639cfeaaf0b97b588"), "Price" : 100 } { "_id" : ... Read More

MongoDB query for counting the distinct values across all documents?

AmitDiwan
Updated on 14-May-2020 10:22:06
For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo718.insertOne( ...    { ...       "id":101, ...       "details": ...       { ...          "OtherDetails": ["Chris", "Mike", "Sam"], "GroupName": ["Group-1"], "Info": [] ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eaae25843417811278f5880") } > db.demo718.insertOne( ...    { ...       "id":102, ...       "details": ...       { ...          "OtherDetails": ["Chris", "David"], "GroupName": ["Group-1"], "Info": [] ...   ... Read More

Aggregation: group date in nested documents (nested object) and display the count?

AmitDiwan
Updated on 14-May-2020 10:20:20
For aggregation, use aggregate() in MongoDB. Group the dates with $group. Let us create a collection with documents −> db.demo717.insertOne( ...    { ...       "shippingdetails": ...       [ ...          { ...             duedate:"2020-04-29 22:33:04", ...          }, ...          { ...             duedate:"2020-03-29 22:33:04", ...          }, ...          { ...             duedate:"2020-04-29 22:33:04", ...          }, ...       ... Read More

Creating hierarchical JSON in MongoDB?

AmitDiwan
Updated on 14-May-2020 10:16:43
Use the following syntax to create hierarchical JSON in MongoDB −db.demo716.insertOne(    {       yourFieldName1,       yourFieldName2,       .       .       N,       "fieldName": {          yourFieldName1,          yourFieldName2,          .          .          N,          "fieldname":          [             {                yourFieldName1,                yourFieldName2,         ... Read More

Removing an object in a child collection in MongoDB?

AmitDiwan
Updated on 14-May-2020 10:12:12
To remove an object in a child collection, use $pull in MongoDB. Let us create a collection with documents −> db.demo715.insertOne({ ...    _id:1, ...    details : ...    [ ...       { 'id' : '101', ...       'Information' : [ ...          { ...             'studentid' : '102', ...             "Name":"Bob" ...          }, ...          { ...             'studentid' : '103', ...             "Name":"Chris" ... Read More

MongoDB compound conditions to fetch documents?

AmitDiwan
Updated on 14-May-2020 10:10:06
Let us create a collection with documents −> db.demo714.insertOne({FirstName:"Chris", LastName:"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2da85324c2c98cc4c2b") } > db.demo714.insertOne({FirstName:"Adam", LastName:"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2e585324c2c98cc4c2c") } > db.demo714.insertOne({FirstName:"David", LastName:"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2ed85324c2c98cc4c2d") } > db.demo714.insertOne({FirstName:"John", LastName:"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea9a2f785324c2c98cc4c2e") }Display all documents from a collection with the help of find() method −> db.demo714.find();This will produce the following output −{ "_id" : ObjectId("5ea9a2da85324c2c98cc4c2b"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5ea9a2e585324c2c98cc4c2c"), "FirstName" : "Adam", "LastName" : "Smith" } { ... Read More

Update tag records in MongoDB quickly

AmitDiwan
Updated on 14-May-2020 10:08:31
Use $ along with update command to update tag records. Let us create a collection with documents −> db.demo713.insertOne( ...    { ...       tags: ...       [ ...          { ...             id:101, ...             Name:"Tag-1" ...          }, ...          { ...             id:102, ...             Name:"Tag-3" ...          }, ...          { ...           ... Read More

How to add a new field to all the documents in a MongoDB collection

AmitDiwan
Updated on 14-May-2020 10:06:37
To add a new field, use $addFields in MongoDB. Let us create a collection with documents −> db.demo712.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f675d33e20ed1097b82") } > db.demo712.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6a5d33e20ed1097b83") } > db.demo712.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85f6e5d33e20ed1097b84") }Display all documents from a collection with the help of find() method −> db.demo712.find();This will produce the following output −{ "_id" : ObjectId("5ea85f675d33e20ed1097b82"), "Name" : "John" } { "_id" : ObjectId("5ea85f6a5d33e20ed1097b83"), "Name" : "Chris" } { "_id" : ObjectId("5ea85f6e5d33e20ed1097b84"), "Name" : "Bob" }Following is the query to add a ... Read More
Advertisements