Found 1349 Articles for MongoDB

How to update record in MongoDB without replacing the existing fields?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

305 Views

You can use $set operator for this Let us first create a collection with documents −> db.updateRecordDemo.insertOne({"StudentName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f95de8cc557214c0e0a") } > db.updateRecordDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f9cde8cc557214c0e0b") } > db.updateRecordDemo.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f9dde8cc557214c0e0c") }Display all documents from a collection with the help of find() method −> db.updateRecordDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbd6f95de8cc557214c0e0a"), "StudentName" : "Larry" } { "_id" : ObjectId("5cbd6f9cde8cc557214c0e0b"), "StudentName" : "David" } { "_id" : ObjectId("5cbd6f9dde8cc557214c0e0c"), "StudentName" : "Mike" }Following is the query to update record in ... Read More

How can I save new Date() in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

108 Views

To save new Date() in MongoDB, you can use new ISODate(). Let us create a collection with documents −> db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2018-01-31 12:34:56')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd67d4de8cc557214c0e04") } > db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2019-02-01 04:01:10')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd67e8de8cc557214c0e05") } > db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2019-04-22 12:36:45')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6805de8cc557214c0e06") }Display all documents from a collection with the help of find() method −> db.saveDateDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd67d4de8cc557214c0e04"),    "UserName" : "John",    "UserLoginDatetime" : ISODate("2018-01-31T12:34:56Z") } {    "_id" : ObjectId("5cbd67e8de8cc557214c0e05"), ... Read More

Create array with MongoDB query?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use the concept of toArray() to create array. Following is the syntax −db.yourCollectonName.find({}, {yourFieldName:1}).toArray();Let us create a collection with documents −> db.createArrayDemo.insertOne({"UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6461de8cc557214c0e00") } > db.createArrayDemo.insertOne({"UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6467de8cc557214c0e01") } > db.createArrayDemo.insertOne({"UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd646cde8cc557214c0e02") } > db.createArrayDemo.insertOne({"UserName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6470de8cc557214c0e03") }Display all documents from a collection with the help of find() method −> db.createArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbd6461de8cc557214c0e00"), "UserName" : "Chris" } { "_id" : ObjectId("5cbd6467de8cc557214c0e01"), ... Read More

Count distinct value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

307 Views

Use the concept of length to count distinct value. Following is the syntax −db.yourCollectionName.distinct("yourFieldName").length;Let us create a collection with documents −> db.countDistinctDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6166de8cc557214c0dfa") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616ade8cc557214c0dfb") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616cde8cc557214c0dfc") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6170de8cc557214c0dfd") } > db.countDistinctDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6175de8cc557214c0dfe") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6181de8cc557214c0dff") }Display all documents from a collection with the help ... Read More

Get the count of the number of documents in a MongoDB Collection?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

158 Views

To get the count of the number of documents in a collection MongoDB, you can use the below syntax −db.getCollectionNames().map(function(anyVariableName) {    return { "yourVariableName": yourVariableName, "count": db[yourVariableName].count() } });Here, we are using ‘test’ database.Let us implement the above syntax to get the count of the number of documents in a MongoDB collection −> db.getCollectionNames().map(function(ColName) { ... return { "ColName": ColName, "TotalDocument": db[ColName].count() } ... });This will produce the following output −[    {       "ColName" : "ConvertStringToDateDemo",       "TotalDocument" : 4    },    {       "ColName" : "Employee_Information",       "TotalDocument" ... Read More

Get index of given element in array field in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

905 Views

You can use $indexOfArray operator for this. Let us create a collection with documents −>db.getIndexDemo.insertOne({"InstructorName":"Chris", "InstructorSubject":["MongoDB", "MySQL", "Java", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd5251de8cc557214c0df8") }Display all documents from a collection with the help of find() method −> db.getIndexDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd5251de8cc557214c0df8"),    "InstructorName" : "Chris",    "InstructorSubject" : [       "MongoDB",       "MySQL",       "Java",       "C++"    ] }Following is the query to get index of given element in an array field in MongoDB −> db.getIndexDemo.aggregate( [ { "$project": { ... Read More

How to query MongoDB using the $ne operator?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

93 Views

To query MongoDB using the $ne operator, following is the syntax −db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty();Let us create a collection with documents −> db.notEqaulToDemo.insertOne({"StudentName":"Larry", "StudentMathMarks":68}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded") } > db.notEqaulToDemo.insertOne({"StudentName":"Chris", "StudentMathMarks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a79de8cc557214c0dee") } > db.notEqaulToDemo.insertOne({"StudentName":"David", "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a89de8cc557214c0def") } > db.notEqaulToDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a97de8cc557214c0df0") }Display all documents from a collection with the help of find() method −> db.notEqaulToDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),    "StudentName" : "Larry",    "StudentMathMarks" : ... Read More

Unset an attribute from a single array element in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

105 Views

Use $unset operator to unset an attribute. Let us first create a collection with documents −> db.unsetAnAttributeDemo.insertOne( ...    { ...       _id: 1, ...       "StudentDetails": [ ...          { ...             "StudentFirstName": "Ramit", ...             "StudentCountryName":"UK" ...          }, ...          { ...             "StudentFirstName": "Bob", ...             "StudentCountryName":"US" ...          }, ...          { ...       ... Read More

Search by property name for any document with that property in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

181 Views

You can use $ne operator for this. Let us first create a collection with documents −> db.searchByPropertyName.insertOne({"FirstName":"Larry", "Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7af7219729fde21ddb5") } > db.searchByPropertyName.insertOne({"FirstName":null, "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7b97219729fde21ddb6") } > db.searchByPropertyName.insertOne({"FirstName":"John", "Age":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7c57219729fde21ddb7") } > db.searchByPropertyName.insertOne({"FirstName":null, "Age":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7d07219729fde21ddb8") } > db.searchByPropertyName.insertOne({"FirstName":"David", "Age":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbaf7df7219729fde21ddb9") }Following is the query to display all documents from the collection with the help of find() prettyprint −> db.searchByPropertyName.find().pretty();This will produce ... Read More

MongoDB query to find the highest numeric value of a column?

Samual Sam
Updated on 02-Jul-2020 11:33:59

132 Views

You can use $not operator along with $type for this. Let us first create a collection with documents −> db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "John", ...       "StudentMathMarks":69 ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cba05727219729fde21ddb1") } > db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "Carol", ...       "StudentMathMarks":"89" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cba059d7219729fde21ddb2") } > db.highestNumericValueOfAColumnDemo.insertOne( ...    { ...       "StudentName": "Chris", ...       "StudentMathMarks":82 ...    } ... ... Read More

Advertisements