Found 1349 Articles for MongoDB

MongoDB query to search for Month and Day only?

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

484 Views

To search for month and day only, use the aggregate framework.Let us first create a collection with documents −> db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2019-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcaeef71edecf6a1f6b2") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-30")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcb7ef71edecf6a1f6b3") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2018-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbcbcef71edecf6a1f6b4") } > db.monthAndDayDemo.insertOne({"LoginDate":new ISODate("2016-04-27")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefbdaaef71edecf6a1f6b5") }Display all documents from a collection with the help of find() method −> db.monthAndDayDemo.find();Output{ "_id" : ObjectId("5cefbcaeef71edecf6a1f6b2"), "LoginDate" : ISODate("2019-04-27T00:00:00Z") } { "_id" : ObjectId("5cefbcb7ef71edecf6a1f6b3"), "LoginDate" : ISODate("2018-04-30T00:00:00Z") } { "_id" ... Read More

Limit number of values in a field using MongoDB?

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

238 Views

To limit number of values in a field, use $slice operator.Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to limit number of values in a field using MongoDB −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": 2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200 ] }

Display the last two values from field with MongoDB

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

71 Views

Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to get the last two values.Here, we have used -ve sign under $slice −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": -2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 1000, 98 ] }

MongoDB query to get last inserted document?

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

12K+ Views

To get last inserted document, use sort() along with limit(1).Let us first create a collection with documents −> db.getLastInsertedDocument.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb17eef71edecf6a1f6a8") } > db.getLastInsertedDocument.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb181ef71edecf6a1f6a9") } > db.getLastInsertedDocument.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb185ef71edecf6a1f6aa") }Display all documents from a collection with the help of find() method −> db.getLastInsertedDocument.find();Output{ "_id" : ObjectId("5cefb17eef71edecf6a1f6a8"), "Name" : "John" } { "_id" : ObjectId("5cefb181ef71edecf6a1f6a9"), "Name" : "Chris" } { "_id" : ObjectId("5cefb185ef71edecf6a1f6aa"), "Name" : "Robert" }Following is the query to get last inserted document −> db.getLastInsertedDocument.find({}).sort({_id:-1}).limit(1);Output{ "_id" ... Read More

MongoDB query to find data from an array inside an object?

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

448 Views

Let us first create a collection with documents −> db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"John",       "CustomerDetails" : {          "CountryName" : [             "AUS"          ],          "isMarried" : [             false          ]       }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefa5eeef71edecf6a1f6a5") } > db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"Carol",       ... Read More

Projection result as an array of selected items in MongoDB?

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

104 Views

Use the distinct() for this, since it finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us first create a collection with documents −> db.projectionListDemo.insertOne({"_id":"1", "Subject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : "1" } > db.projectionListDemo.insertOne({"_id":"2", "Subject":["MongoDB", "C", "C++"]}); { "acknowledged" : true, "insertedId" : "2" } > db.projectionListDemo.insertOne({"_id":"3", "Subject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : "3" }Display all documents from a collection with the help of find() method −> db.projectionListDemo.find().pretty();Output{ "_id" : "1", "Subject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ... Read More

Removing an array element from MongoDB collection using update() and $pull

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

131 Views

Let us first create a collection with documents -> db.removingAnArrayElementDemo.insertOne({"UserMessage":["Hi", "Hello", "Bye"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef97bdef71edecf6a1f6a4") }Display all documents from a collection with the help of find() method -> db.removingAnArrayElementDemo.find().pretty();Output{    "_id" : ObjectId("5cef97bdef71edecf6a1f6a4"),    "UserMessage" : [       "Hi",       "Hello",       "Bye"    ] }Following is the query to remove an array element from MongoDB -> db.removingAnArrayElementDemo.update(    {_id:ObjectId("5cef97bdef71edecf6a1f6a4")},    { "$pull": { "UserMessage": "Hello" } } ); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us check the document once again:> db.removingAnArrayElementDemo.find().pretty();Output{   ... Read More

Write an equality in MongoDB without using $eq operator

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

66 Views

Let us first create a collection with documents -> db.operatorDemo.insertOne({"StudentSubject":["MongoDB", "MySQL", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef94eaef71edecf6a1f6a2") } > db.operatorDemo.insertOne({"StudentSubject":["Java", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cef94faef71edecf6a1f6a3") }Display all documents from a collection with the help of find() method -> db.operatorDemo.find().pretty();Output{    "_id" : ObjectId("5cef94eaef71edecf6a1f6a2"),    "StudentSubject" : [       "MongoDB",       "MySQL",       "Java"    ] } {    "_id" : ObjectId("5cef94faef71edecf6a1f6a3"),    "StudentSubject" : [       "Java",       "C",       "C++"    ] }Following is the query for ... Read More

MongoDB query to find a value from JSON like data?

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

4K+ Views

To fund a value from JSON data, use the find() along with dot(.) notation. Let us first create a collection with documents −> db.findValueFromJsonDemo.insertOne(    {       "UserDetails": [{          "_id": new ObjectId(),          "UserName": "Carol",          "UserMessage": "Hi"       }],       "UserFriendsName": ["John", "Sam"]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdf8a4cbf3115999ed511fd") }Following is the query to display all documents from a collection with the help of find() method −> db.findValueFromJsonDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

MongoDB Limit fields and slice projection together?

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

208 Views

Use the $slice operator. Let us first create a collection with documents −> db.limitAndSliceProjectionDemo.insertOne(    {       "_id" : 101,       "UserName" : "Carol",       "UserAge" : 26,       "UserMesssage" : [          "Hi",          "Hello",          "Bye",          "Awesome",          "Good",          "Bad",          "Nice",          "Good Night",          "Good Morning"       ]    } ); { "acknowledged" : true, "insertedId" : ... Read More

Advertisements