Found 6702 Articles for Database

How to aggregate array documents in MongoDB?

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

147 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

102 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

446 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

481 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

252 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

252 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

299 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

Find minimum value with MongoDB?

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

257 Views

To get the minimum value, use sort() along with limit(1). Let us first create a collection with documents −> db.findMinimumValueDemo.insertOne({"Value":1004}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd61abf3115999ed51208") } > db.findMinimumValueDemo.insertOne({"Value":983}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd61ebf3115999ed51209") } > db.findMinimumValueDemo.insertOne({"Value":990}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd622bf3115999ed5120a") } > db.findMinimumValueDemo.insertOne({"Value":1093}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd626bf3115999ed5120b") } > db.findMinimumValueDemo.insertOne({"Value":10090}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfd62fbf3115999ed5120c") }Following is the query to display all documents from a collection with the help of find() method −> db.findMinimumValueDemo.find();This will produce the following ... Read More

MongoDB query for Partial Object in an array

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

348 Views

Let us first create a collection with documents −> db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":1, "StudentName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfcf55bf3115999ed51206") } > db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":2, "StudentName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfcf55bf3115999ed51207") }Following is the query to display all documents from a collection with the help of find() method −> db.queryForPartialObjectDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cdfcf55bf3115999ed51206"),    "StudentDetails" : [       {          "StudentId" : 1,          "StudentName" : "Chris"       }    ] } {    "_id" ... Read More

Get the size of a collection in MongoDB using conditions?

George John
Updated on 30-Jul-2019 22:30:26

139 Views

Let us first create a collection with documents −> dbmongoDBCollectionSizeDemoinsertOne({"Name":"John", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf23e3db64a577be5a2bc16") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Chris", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf23e45b64a577be5a2bc17") } > dbmongoDBCollectionSizeDemoinsertOne({"Name":"Robert", "Age":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf23e4db64a577be5a2bc18") }Following is the query to display all documents from a collection with the help of find() method −> dbmongoDBCollectionSizeDemofind();This will produce the following document −{ "_id" : ObjectId("5cf23e3db64a577be5a2bc16"), "Name" : "John", "Age" : 23 } { "_id" : ObjectId("5cf23e45b64a577be5a2bc17"), "Name" : "Chris", "Age" : 24 } { "_id" : ObjectId("5cf23e4db64a577be5a2bc18"), "Name" : "Robert", "Age" ... Read More

Advertisements