Found 6705 Articles for Database

Find minimum value with MongoDB?

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

354 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

449 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

206 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

181 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

428 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

169 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

235 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

How to print results of script in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:26

1K+ Views

We will use printjson() for this. Let us first create a collection with documents −> dbprintResultScriptDemoinsertOne({"StudentName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c02b64a577be5a2bc0b") } > dbprintResultScriptDemoinsertOne({"StudentName":"Carol", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c09b64a577be5a2bc0c") } > dbprintResultScriptDemoinsertOne({"StudentName":"David", "StudentAge":19}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf22c11b64a577be5a2bc0d") }Following is the query to display all documents from a collection with the help of find() method −> dbprintResultScriptDemofind();This will produce the following document −{ "_id" : ObjectId("5cf22c02b64a577be5a2bc0b"), "StudentName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cf22c09b64a577be5a2bc0c"), "StudentName" : "Carol", "StudentAge" : 20 } { "_id" ... Read More

Group all documents with common fields in MongoDB?

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

382 Views

For this, use the $addToSet operator. Let us first create a collection with documents −> db.findDocumentWithCommonFieldsDemo.insertOne(    {       "UserId":1,       "UserName":"Carol"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8ebebf3115999ed51200") } > db.findDocumentWithCommonFieldsDemo.insertOne(    {       "UserId":2,       "UserName":"David"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8ebebf3115999ed51201") } > > db.findDocumentWithCommonFieldsDemo.insertOne(    {       "UserId":1,       "UserName":"Sam"    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8ebebf3115999ed51202") }Following is the query to display all documents from a collection ... Read More

Compare multiple properties in MongoDB?

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

174 Views

To compare multiple properties, use the $where operator. Let us first create a collection with documents −> dbcomparingMultiplePropertiesDemoinsertOne({"Values":[10, 70, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf228fcb64a577be5a2bc0a") }Following is the query to display all documents from a collection with the help of find() method −> dbcomparingMultiplePropertiesDemofind()pretty();This will produce the following document −{    "_id" : ObjectId("5cf228fcb64a577be5a2bc0a"),    "Values" : [       10,       70,       60    ] }Case 1: If condition becomes true then you will get an array otherwise nothing will get displayed Following is the query to compare multiple ... Read More

Advertisements