Found 6705 Articles for Database

MongoDB query to replace value with aggregation?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

481 Views

Use aggregate framework along with $literal operator. Let us first create a collection with documents −> db.replaceValueDemo.insertOne(    {       _id : 100,       "EmployeeName" :"Chris",       "EmployeeOtherDetails": {          "EmployeeDesignation" : "HR",          "EmployeeAge":27       }    } ); { "acknowledged" : true, "insertedId" : 100 } > db.replaceValueDemo.insertOne(    {       _id : 101,       "EmployeeName" :"David",       "EmployeeOtherDetails": {          "EmployeeDesignation" : "Tester",          "EmployeeAge":26       }    } ... Read More

How to search for string or number in a field with MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

961 Views

You can use $in operator for this. Let us first create a collection with documents −> db.searchForStringOrNumberDemo.insertOne(    {       "_id": new ObjectId(),       "StudentName": "Larry",       "StudentDetails": {          "StudentMarks": {             "StudentMongoDBMarks": [44]          }       }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce2407c36e8b255a5eee944") } > db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": ["44"] } } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce240f036e8b255a5eee945") }Following is ... Read More

MongoDB query to return specific fields from an array?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

697 Views

To return specific fields, use aggregate $project. Let us first create a collection with documents −> db.returnSpecificFieldDemo.insertOne(    {       "StudentId":1,       "StudentDetails": [          {             "StudentName":"Larry",             "StudentAge":21,             "StudentCountryName":"US"          },          {             "StudentName":"Chris",             "StudentAge":23,             "StudentCountryName":"AUS"          }       ]    } ); {    "acknowledged" ... Read More

How to aggregate array documents in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

207 Views

For this, use the aggregate framework. Let us first create a collection with documents −> db.aggregateArrayDemo.insertOne(    {       "_id":100,       "UserDetails": [          {             "UserName": "John",             "UserLoginYear":2010          },          {             "UserName": "Carol",             "UserLoginYear":2019          }       ]    } ); { "acknowledged" : true, "insertedId" : 100 }Following is the query to display all documents from a collection with the help of find() method −> db.aggregateArrayDemo.find().pretty();This will produce the following output −{    "_id" : 100,    "UserDetails" : [       {          "UserName" : "John",          "UserLoginYear" : 2010       },       {          "UserName" : "Carol",          "UserLoginYear" : 2019       }    ] }Following is the query to aggregate array documents −> db.aggregateArrayDemo.aggregate([    {       $match: { _id: 100 }    },    {       $project: {          Minimum: { $min: "$UserDetails.UserLoginYear" },          Maximum: { $max: "$UserDetails.UserLoginYear" }       }    } ]);This will produce the following output −{ "_id" : 100, "Minimum" : 2010, "Maximum" : 2019 }

How to get the matching document inside an array in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

160 Views

To get the matching document, use $elemMatch. Let us first create a collection with documents −> db.getMatchingDocumentDemo.insertOne(    {       _id :1,       "UserDetails":[          {             "UserName":"John",             "UserAge":23          }       ]    } ); { "acknowledged" : true, "insertedId" : 1 } > db.getMatchingDocumentDemo.insertOne( { _id :2, "UserDetails":[ { "UserName":"Larry", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 2 }Following is the query to display all documents from a collection with the ... Read More

How to add an extra field in a sub document in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

547 Views

You can use update(). Let us first create a collection with documents −> db.addExtraFieldDemo.insertOne(    {       "_id": 1,       "UserName": "Larry" ,       "UserOtherDetails":[          {             "UserCountryName": "US",             "UserAge":23          },          {             "UserCountryName": "UK",             "UserAge":24          }       ]    } ); { "acknowledged" : true, "insertedId" : 1 }Following is the query to ... Read More

How to delete multiple documents in MongoDB using deleteMany()?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

589 Views

Let us first create a collection with documents −> db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce00b07bf3115999ed51214") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce00b0bbf3115999ed51215") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce00b0fbf3115999ed51216") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce00b12bf3115999ed51217") } > db.deleteMultipleDocumentsDemo.insertOne({"StudentFirstName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce00b18bf3115999ed51218") }Following is the query to display all documents from a collection with the help of find() method −> db.deleteMultipleDocumentsDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce00b07bf3115999ed51214"), "StudentFirstName" : "Larry" } { ... Read More

How to use $regex in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

300 Views

Following is the syntax to use $regex in MongoDB −db.yourCollectionName.find({yourFieldName: { $regex: yourValue}});Let us first create a collection with documents −> db.regularExpressionDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc25bf3115999ed51210") } > db.regularExpressionDemo.insertOne({"UserName":"JOHN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc2ebf3115999ed51211") } > db.regularExpressionDemo.insertOne({"UserName":"john"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc35bf3115999ed51212") } > db.regularExpressionDemo.insertOne({"UserName":"JoHn"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc3ebf3115999ed51213") }Following is the query to display all documents from a collection with the help of find() method −> db.regularExpressionDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdffc25bf3115999ed51210"), "UserName" : "John" } { ... Read More

Find the exact match in array without using the $elemMatch operator in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

325 Views

As an alternative, use the $eq operator. Let us first create a collection with documents −> db.equalDemo.insertOne({_id:1, "StudentFriendNames":["John", "Carol", "Sam"]}); { "acknowledged" : true, "insertedId" : 1 } > db.equalDemo.insertOne({_id:2, "StudentFriendNames":null}); { "acknowledged" : true, "insertedId" : 2 } > db.equalDemo.insertOne({_id:3, "StudentFriendNames":["Carol"]}); { "acknowledged" : true, "insertedId" : 3 } > db.equalDemo.insertOne({_id:4, "StudentFriendNames":["Sam"]}); { "acknowledged" : true, "insertedId" : 4 }Following is the query to display all documents from a collection with the help of find() method −> db.equalDemo.find();This will produce the following output −{ "_id" : 1, "StudentFriendNames" : [ "John", "Carol", "Sam" ] } { "_id" : 2, ... Read More

MongoDB query to exclude both the fields with FALSE

Anvi Jain
Updated on 30-Jul-2019 22:30:26

397 Views

Use $or operator along with $expr operator for this. Let us first create a collection with documents, wherein one of fields is isMarried having true of false value −> db.orTwoFieldsDemo.insertOne({"isLiveInUS":true, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd86abf3115999ed5120d") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":true, "isMarried":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd876bf3115999ed5120e") } > db.orTwoFieldsDemo.insertOne({"isLiveInUS":false, "isMarried":false}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd87dbf3115999ed5120f") }Following is the query to display all documents from a collection with the help of find() method −> db.orTwoFieldsDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdfd86abf3115999ed5120d"), "isLiveInUS" : true, "isMarried" : false ... Read More

Advertisements