Found 1349 Articles for MongoDB

Perform conditional upserts or updates in MongoDB

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

98 Views

For conditional upserts or updates, you can use $max operator. Let us first create a collection with documents>db.conditionalUpdatesDemo.insertOne({"_id":100, "StudentFirstScore":89, "StudentSecondScore":78, "BiggestScore":89}); { "acknowledged" : true, "insertedId" : 100 } >db.conditionalUpdatesDemo.insertOne({"_id":101, "StudentFirstScore":305, "StudentSecondScore":560, "BiggestScore":1050}); { "acknowledged" : true, "insertedId" : 101 } >db.conditionalUpdatesDemo.insertOne({"_id":103, "StudentFirstScore":560, "StudentSecondScore":789, "BiggestScore":880}); { "acknowledged" : true, "insertedId" : 103 } Following is the query to display all documents from a collection with the help of find() method: > db.conditionalUpdatesDemo.find().pretty();This will produce the following output{    "_id" : 100,    "StudentFirstScore" : 89,    "StudentSecondScore" : 78,    "BiggestScore" : 89 } {    "_id" : 101,   ... Read More

Pull and add to set at the same time with MongoDB? Is it Possible?

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

647 Views

Yes, you can use pull and add at the same time with $addToSet and $pull operator. Let us first create a collection with documents> db.pullAndAddToSetDemo.insertOne({StudentScores : [78, 89, 90]} ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a797e15e86fd1496b38af") }Following is the query to display all documents from a collection with the help of find() method> db.pullAndAddToSetDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9a797e15e86fd1496b38af"),    "StudentScores" : [       78,       89,       90    ] }Following is the query to pull and addtoset at the same time in MongoDB> var ... Read More

Find oldest/ youngest post in MongoDB collection?

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

905 Views

To find oldest/youngest post in MongoDB collection, you can use sort(). Let’s say you have a document with a field “UserPostDate” and you need to get the oldest and youngest post. For that, Let us first create a collection with documents>db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Larry@123", "UserName":"Larry", "UserPostDate":new ISODate('2019-03-27 12:00:00')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a700f15e86fd1496b38ab") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Sam@897", "UserName":"Sam", "UserPostDate":new ISODate('2012-06-17 11:40:30')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a703815e86fd1496b38ac") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"David@777", "UserName":"David", "UserPostDate":new ISODate('2018-01-31 10:45:35')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a705e15e86fd1496b38ad") } >db.getOldestAndYoungestPostDemo.insertOne({"UserId":"Chris@909", "UserName":"Chris", "UserPostDate":new ISODate('2017-04-14 04:12:04')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a708915e86fd1496b38ae") }Following ... Read More

Increment MongoDB value inside a nested array

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

115 Views

To increment a value inside a nested array, use positional operator ($). Let us first create a collection with documents> db.incrementInNestedArrayDemo.insertOne( ... { ...    "StudentId":100, ...    "ProjectDetails": ...    [ ...       {"ProjectId":567778888, ...          "TeamSize":4 ...       }, ...       { ...          "ProjectId":67888999, ...          "TeamSize":2 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a6b7b15e86fd1496b38aa") }Following is the query to display all documents from a collection with the help ... Read More

Only insert if a value is unique in MongoDB else update

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

630 Views

You can use upsert i.e. whenever you insert a value and it already exist then update would be performed. If the value does not already exist then it would get inserted.Let us first create a collection with documents> db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Larry", "StudentAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a633815e86fd1496b38a4") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Mike", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a634a15e86fd1496b38a5") } > db.onlyInsertIfValueIsUniqueDemo.insertOne({"StudentName":"Sam", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a635015e86fd1496b38a6") }Following is the query to display all documents from a collection with the help of find() method> db.onlyInsertIfValueIsUniqueDemo.find().pretty();This will produce the following output{    "_id" ... Read More

How to count the number of documents in a MongoDB collection?

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

453 Views

Following is the syntax to count the number of documents in a MongoDB collectionlet anyVariableName= db.getCollection(‘yourCollectionName’); yourVariableName.count();Let us first create a collection with documents> db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a5e2015e86fd1496b38a1") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Ramit", "CustomerAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a5e3515e86fd1496b38a2") } >db.countNumberOfDocumentsDemo.insertOne({"CustomerName":"Adam", "CustomerAge":27, "CustomerCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a5e4c15e86fd1496b38a3") }Following is the query to display all documents from a collection with the help of find() method> db.countNumberOfDocumentsDemo.find().pretty();This will produce the following output{ "_id" : ObjectId("5c9a5e2015e86fd1496b38a1"), "CustomerName" : "Bob" } {    "_id" : ObjectId("5c9a5e3515e86fd1496b38a2"),    "CustomerName" : "Ramit",    "CustomerAge" ... Read More

How to return only a single property “_id” in MongoDB?

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

1K+ Views

Following is the syntax to return only a single property _id in MongoDBdb.yourCollectionName.find({}, {"_id": 1}).pretty();Let us first create a collection with documents> db.singlePropertyIdDemo.insertOne({"_id":101, "UserName":"Larry", "UserAge":21}); { "acknowledged" : true, "insertedId" : 101 } > db.singlePropertyIdDemo.insertOne({"_id":102, "UserName":"Mike", "UserAge":26}); { "acknowledged" : true, "insertedId" : 102 } > db.singlePropertyIdDemo.insertOne({"_id":103, "UserName":"Chris", "UserAge":24}); { "acknowledged" : true, "insertedId" : 103 } > db.singlePropertyIdDemo.insertOne({"_id":104, "UserName":"Robert", "UserAge":23}); { "acknowledged" : true, "insertedId" : 104 } > db.singlePropertyIdDemo.insertOne({"_id":105, "UserName":"John", "UserAge":27}); { "acknowledged" : true, "insertedId" : 105 }Following is the query to display all documents from a collection with the help of find() method> db.singlePropertyIdDemo.find().pretty();This will produce ... Read More

Is it possible to rename _id field after MongoDB group aggregation?

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

2K+ Views

Yes, it is possible to rename using aggregation. Let us first create a collection with documents> db.renameIdDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a1760353decbc2fc927c5") } > db.renameIdDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a1765353decbc2fc927c6") } > db.renameIdDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a176b353decbc2fc927c7") }Following is the query to display all documents from a collection with the help of find() method> db.renameIdDemo.find();This will produce the following output{ "_id" : ObjectId("5c9a1760353decbc2fc927c5"), "StudentName" : "Chris" } { "_id" : ObjectId("5c9a1765353decbc2fc927c6"), "StudentName" : "Robert" } { "_id" : ObjectId("5c9a176b353decbc2fc927c7"), "StudentName" : "David" }Following is the query to ... Read More

Query MongoDB with length criteria?

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

120 Views

To query MongoDB with length criteria, you can use regex. Following is the syntaxdb.yourCollectionName.find({ ‘yourFieldName’: { $regex: /^.{yourLengthValue1, yourLengthValue2}$/ } });Let us create a collection with documents. Following is the query> db.queryLengthDemo.insertOne({"StudentFullName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a01ae353decbc2fc927c0") } > db.queryLengthDemo.insertOne({"StudentFullName":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a01b4353decbc2fc927c1") } > db.queryLengthDemo.insertOne({"StudentFullName":"David Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a01c2353decbc2fc927c2") } > db.queryLengthDemo.insertOne({"StudentFullName":"Robert Taylor"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a01e2353decbc2fc927c3") } > db.queryLengthDemo.insertOne({"StudentFullName":"Chris Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9a01f1353decbc2fc927c4") }Following is the query to display ... Read More

How to iterate over all MongoDB databases?

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

324 Views

To iterate over all MongoDB databases, you need to switch your database to admin. Following is the query to switch to admin and get information about all the databases> switchDatabaseAdmin = db.getSiblingDB("admin"); admin > allDatabaseName = switchDatabaseAdmin.runCommand({ "listDatabases": 1 }).databases;This will produce the following output[    {       "name" : "admin",       "sizeOnDisk" : 495616,       "empty" : false    },    {       "name" : "config",       "sizeOnDisk" : 98304,       "empty" : false    },    {       "name" : "local",       "sizeOnDisk" : 73728,       "empty" : false    },    {       "name" : "sample",       "sizeOnDisk" : 1335296,       "empty" : false    },    {       "name" : "sampleDemo",       "sizeOnDisk" : 278528,       "empty" : false    },    {       "name" : "studentSearch",       "sizeOnDisk" : 262144,       "empty" : false    },    {       "name" : "test",       "sizeOnDisk" : 8724480,       "empty" : false    } ]

Advertisements