Found 1349 Articles for MongoDB

How to use $regex in MongoDB?

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

250 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

250 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

294 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

253 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

342 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

138 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

Find documents that contains specific field in MongoDB?

Chandu yadav
Updated on 30-Jul-2019 22:30:26

127 Views

For this, use the $exists operator. Let us first create a collection with documents −>dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product1":10, "Pr oduct2":50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf2385bb64a577be5a2bc14") } >dbfindDocumentContainsSpecificFieldDemoinsertOne({"ProductPrices":{"Product3":150, "P roduct7":100, "Product5":250}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf2387eb64a577be5a2bc15") }Following is the query to display all documents from a collection with the help of find() method −> dbfindDocumentContainsSpecificFieldDemofind()pretty();This will produce the following document −{    "_id" : ObjectId("5cf2385bb64a577be5a2bc14"),    "ProductPrices" : {       "Product1" : 10,       "Product2" : 50    } } {    "_id" : ObjectId("5cf2387eb64a577be5a2bc15"),    "ProductPrices" : {       ... Read More

Getting only the first item for an array property in MongoDB?

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

314 Views

Use $slice operator for this. Let us first create a collection with documents −> db.gettingFirstItemInArrayDemo.insertOne(    {       "UserId": 101,       "UserName":"Carol",       "UserOtherDetails": [          {"UserFriendName":"Sam"},          {"UserFriendName":"Mike"},          {"UserFriendName":"David"},          {"UserFriendName":"Bob"}       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfca52bf3115999ed51205") }Following is the query to display all documents from a collection with the help of find() method −> db.gettingFirstItemInArrayDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cdfca52bf3115999ed51205"),    "UserId" : 101, ... Read More

How to limit the amount of characters returned from a field in a MongoDB query?

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

109 Views

For this, use MongoDB $substr. Let us first create a collection with documents −> dblimitTheNumberOfCharactersDemoinsertOne({"Title":"MongoDB is No SQL database"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf23013b64a577be5a2bc0e") } > dblimitTheNumberOfCharactersDemoinsertOne({"Title":"MySQL is a relational database"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf2302db64a577be5a2bc0f") } Following is the query to display all documents from a collection with the help of find() method −> dblimitTheNumberOfCharactersDemofind()pretty();This will produce the following document −{    "_id" : ObjectId("5cf23013b64a577be5a2bc0e"),    "Title" : "MongoDB is No SQL database" } {    "_id" : ObjectId("5cf2302db64a577be5a2bc0f"),    "Title" : "MySQL is a relational database" }Following is the ... Read More

How to pop a single value in MongoDB?

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

186 Views

You can use pop() for this. Let us first create a collection with documents −> db.persistChangeDemo.insertOne({"Name" : "Larry", "CreditScore": [500, 700, 760, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfc52cbf3115999ed51203") }Following is the query to display all documents from a collection with the help of find() method −> db.persistChangeDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cdfc52cbf3115999ed51203"),    "Name" : "Larry",    "CreditScore" : [       500,       700,       760,       100    ] }Following is the query to pop a value −> myDocument.CreditScore.pop(); 100Let us save ... Read More

Advertisements