Found 1359 Articles for MongoDB

Difference between “now” and a given date with MongoDB?

AmitDiwan
Updated on 30-Jun-2020 07:42:37

956 Views

To get the difference between dates in MongpDB, use aggregate(). Let us create a collection with documents −> db.demo734.insertOne({GivenDate:new ISODate("2020-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f1a57bb72a10bcf064e") } > db.demo734.insertOne({GivenDate:new ISODate("2020-02-20")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f2157bb72a10bcf064f") } > db.demo734.insertOne({GivenDate:new ISODate("2010-12-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead4f2b57bb72a10bcf0650") } > db.demo734.insertOne({GivenDate:new ISODate("2020-05-01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ead506f57bb72a10bcf0651") }Display all documents from a collection with the help of find() method −> db.demo734.find();This will produce the following output −{ "_id" : ObjectId("5ead4f1a57bb72a10bcf064e"), "GivenDate" : ISODate("2020-01-10T00:00:00Z") } { "_id" : ObjectId("5ead4f2157bb72a10bcf064f"), "GivenDate" ... Read More

How to move an array of embedded documents up to parent and change key/value with aggregation pipeline?

AmitDiwan
Updated on 15-May-2020 09:26:12

874 Views

Use $replaceRoot in MongoDB aggregation. The $replaceRoot replaces the input document with the specified document. The operation replaces all existing fields in the input document, including the _id field. Let us create a collection with documents −> db.demo733.insertOne( ...    { ...       "SubjectDetails": ...       [ ...          { ...             SubjectName:"MongoDB", ...             "Marks":85 ...          }, ...          { ...             SubjectName:"MySQL", ...             ... Read More

How to add a field with specific datatype (list, object) in an existing MongoDB document?

AmitDiwan
Updated on 15-May-2020 09:23:53

362 Views

You can use $set. Let us create a collection with documents −> db.demo732.insertOne({_id:1, Language:"English"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo732.insertOne({_id:2, Language:"Hindi"}); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo732.find();This will produce the following output −{ "_id" : 1, "Language" : "English" } { "_id" : 2, "Language" : "Hindi" }Following is the query to add a field with specific datatype (list, object) in an existing MongoDB document −> db.demo732.update({_id:1}, ... { $set:{details:{'subjectName':"MongoDB"}, studentDetails:[{Name:"David"}, {CountryName:"US"}]}}) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : ... Read More

How to display a specific field in array using $project in MongoDB and ignore other fields?

AmitDiwan
Updated on 15-May-2020 09:22:15

701 Views

To display a specific field, use $project along with $unwind. To ignore a field, set to 0. Let us create a collection with documents −> db.demo731.insertOne({ "ProductInformation": [ { ProductId:"Product-1", ProductPrice:80 }, { ProductId:"Product-2", ProductPrice:45 }, { ProductId:"Product-3", ProductPrice:50 } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac5efd56e85a39df5f6341") }Display all documents from a collection with the help of find() method −> db.demo731.find();This will produce the following output −{ "_id" : ObjectId("5eac5efd56e85a39df5f6341"), "ProductInformation" : [ { "ProductId" : "Product-1", "ProductPrice" : 80 }, { "ProductId" : "Product-2", "ProductPrice" : 45 }, { "ProductId" : "Product-3", "ProductPrice" : ... Read More

Match MongoDB documents with field value greater than a specific number and fetch them?

AmitDiwan
Updated on 15-May-2020 09:20:25

392 Views

To match, use $match in MongoDB. For values greater than a specific number, use $gt. Let us create a collection with documents −> db.demo730.insertOne({"Name" : "Chris", "Marks" : 33 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54cd56e85a39df5f6339") } > db.demo730.insertOne({ "Name" : "David", "Marks" : 89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54cd56e85a39df5f633a") } > db.demo730.insertOne({ "Name" : "Chris", "Marks" : 45 }); {    "acknowledged" : true,    "insertedId" : ObjectId("5eac54ce56e85a39df5f633b") }Display all documents from a collection with the help of find() method −> db.demo730.find();This will produce the following output −{ "_id" : ObjectId("5eac54cd56e85a39df5f6339"), "Name" ... Read More

Mass insertion in MongoDB

AmitDiwan
Updated on 15-May-2020 09:18:39

65 Views

For mass insertion, use the concept of insertMany() in MongoDB. The insertMany() inserts multiple documents into a collection.Let us create a collection with documents −> db.demo729.insertMany( [ ...    { BankName:"HDFC Bank", cardType:"Credit", "CustomerName":[{Name:"Chris", Age:25}]}, ...    { BankName:"ICICI Bank", cardType:"Debit", "CustomerName":[{Name:"Bob", Age:22}]}, ...    { BankName:"Kotak Bank", cardType:"Debit", "CustomerName":[{Name:"David", Age:23}]} ... ] ); {    "acknowledged" : true,    "insertedIds" : [       ObjectId("5eac510d56e85a39df5f6333"),       ObjectId("5eac510d56e85a39df5f6334"),       ObjectId("5eac510d56e85a39df5f6335")    ] }Display all documents from a collection with the help of find() method −> db.demo729.find().pretty();This will produce the following output −{    "_id" : ... Read More

Find MongoDB records with Price less than a specific value

AmitDiwan
Updated on 15-May-2020 09:16:20

304 Views

To check the records with Price less than a specific value, use $lt. Let us create a collection with documents −> db.demo728.insertOne({Price:75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab413c43417811278f589b") } > db.demo728.insertOne({Price:59}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414043417811278f589c") } > db.demo728.insertOne({Price:79}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414543417811278f589d") } > db.demo728.insertOne({Price:89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab414843417811278f589e") }Display all documents from a collection with the help of find() method −> db.demo728.find();This will produce the following output −{ "_id" : ObjectId("5eab413c43417811278f589b"), "Price" : 75 } { "_id" : ObjectId("5eab414043417811278f589c"), "Price" : ... Read More

MongoDB query to search for string like “@email” in the field values

AmitDiwan
Updated on 15-May-2020 09:14:43

1K+ Views

Search for email string using MongoDB find(). Let us create a collection with documents −> db.demo727.insertOne({UserId:"John@email.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab375f43417811278f5898") } > db.demo727.insertOne({UserId:"John@yahoo.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab376043417811278f5899") } > db.demo727.insertOne({UserId:"Chris@EMAIL.com"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5eab376143417811278f589a") }Display all documents from a collection with the help of find() method −> db.demo727.find();This will produce the following output −{ "_id" : ObjectId("5eab375f43417811278f5898"), "UserId" : "John@email.com" } { "_id" : ObjectId("5eab376043417811278f5899"), "UserId" : "John@yahoo.com" } { "_id" : ObjectId("5eab376143417811278f589a"), "UserId" : "Chris@EMAIL.com" }Following is the query to search for @email like ... Read More

How can I count the documents in an array based on the value of a specific field?

AmitDiwan
Updated on 15-May-2020 09:12:54

100 Views

For such match and count, use $match in MongoDB. Let us create a collection with documents −> db.demo726.insertOne( ...    { ...       id:101, ...       "details": [ ...          { ...             Name:"Chris" ... ...          }, ...          { ...             Name:"Chris" ... ...          }, ...          { ...             Name:"Bob" ...          } ...       ] ... ... Read More

Set filtering conditions for nested array in MongoDB

AmitDiwan
Updated on 15-May-2020 09:10:01

1K+ Views

To set filtering conditions, use $filter and $cond in MongoDB aggregate(). The $filter selects a subset of an array to return based on the specified condition. Let us create a collection with documents −> db.demo725.insertOne( ...    { ... ...       "details": { ... ...          "userMessages": [ ...             { ...                "Messages": [ ...                   { "Message": "Hello" }, ...                   { "Message": "How" }, ... ... Read More

Previous 1 ... 3 4 5 6 7 ... 136 Next
Advertisements