Found 1349 Articles for MongoDB

MongoDB Aggregate Second Element from Input Element?

AmitDiwan
Updated on 12-May-2020 06:57:19

73 Views

To aggregate second element from input element, use mapReduce(). Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. Let us create a collection with documents −> db.demo621.insert({ _id: 101, Name1: "John", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 102, Name1: "Bob", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 103, Name1: "Chris", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 104, Name1: "Sam", Name2: "John" }); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> ... Read More

Aggregate by country, state and city in a MongoDB collection with multiple documents

AmitDiwan
Updated on 12-May-2020 06:54:24

754 Views

Aggregation operations group values from multiple documents together, and can perform a variety of operations on the grouped data to return a single result.To aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo620.insertOne({"Country":"IND", "City":"Delhi", state:"Delhi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8de96c954c74be91e6a1") } > db.demo620.insertOne({"Country":"IND", "City":"Bangalore", state:"Karnataka"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e336c954c74be91e6a3") } > db.demo620.insertOne({"Country":"IND", "City":"Mumbai", state:"Maharashtra"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9a8e636c954c74be91e6a4") }Display all documents from a collection with the help of find() method −> db.demo620.find();This will produce the following output −{ "_id" : ObjectId("5e9a8de96c954c74be91e6a1"), ... Read More

MongoDB – Fix “Failed to convert from type String to type Date”?

AmitDiwan
Updated on 12-May-2020 14:57:13

393 Views

To fix this, use $dateFromString in MongoDB aggregate(). The $dateFromString converts a date/time string to a date object.Let us create a collection with documents −> db.demo619.insertOne({"DueDate":"10-10-2020"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7846c954c74be91e69e") } > db.demo619.insertOne({"DueDate":"12-01-2019"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7996c954c74be91e69f") } > db.demo619.insertOne({"DueDate":"28-10-2010"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d7ab6c954c74be91e6a0") }Display all documents from a collection with the help of find() method −> db.demo619.find();This will produce the following output −{ "_id" : ObjectId("5e99d7846c954c74be91e69e"), "DueDate" : "10-10-2020" } { "_id" : ObjectId("5e99d7996c954c74be91e69f"), "DueDate" : "12-01-2019" } { "_id" : ObjectId("5e99d7ab6c954c74be91e6a0"), "DueDate" : ... Read More

Can we work MongoDB findOne() with long type _id?

AmitDiwan
Updated on 12-May-2020 06:47:03

119 Views

Yes, we can do that using the NumberLong() datatype in MongoDB. Let us create a collection with documents −> db.demo618.insertOne({_id:NumberLong("6336366454"), Name:"Chris"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366454") } > db.demo618.insertOne({_id:NumberLong("6336366455"), Name:"David"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366455") } > db.demo618.insertOne({_id:NumberLong("6336366456"), Name:"Bob"}); { "acknowledged" : true, "insertedId" : NumberLong("6336366456") }Display all documents from a collection with the help of find() method −> db.demo618.find();This will produce the following output −{ "_id" : NumberLong("6336366454"), "Name" : "Chris" } { "_id" : NumberLong("6336366455"), "Name" : "David" } { "_id" : NumberLong("6336366456"), "Name" : "Bob" }Following is the query to implement MongoDB findOne() ... Read More

Group query upon nested object in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:45:23

231 Views

For this, use dot notation along with $group in MongoDB. Let us create a collection with documents −> db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "Chris", ...          "Age":32, ...          "Project":"Online Library Management System" ...       } ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99d2b86c954c74be91e69b") } > > db.demo617.insertOne( ...    { ... ...       "clientDetails": { ...          "Name": "David", ...          "Age":34, ...     ... Read More

How can I aggregate collection and group by field count in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:43:28

574 Views

In MongoDB aggregate(), use $group and aggregate collection. Let us create a collection with documents −> db.demo616.insertOne({"details":{"Name":"Chris", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfac65492f6c60d00283") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":22}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb065492f6c60d00284") } > db.demo616.insertOne({"details":{"Name":"Bob", "Age":23}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfb865492f6c60d00285") } > db.demo616.insertOne({"details":{"Name":"Sam", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfbd65492f6c60d00286") } > db.demo616.insertOne({"details":{"Name":"Chris", "Age":24}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e99bfc165492f6c60d00287") }Display all documents from a collection with the help of find() method −> db.demo616.find();This will produce the following output −{ "_id" ... Read More

How to use $type in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:41:09

82 Views

The $type selects documents where the value of the field is an instance of the specified BSON type. Let us create a collection with documents −> db.demo615.insert({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"100"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":"300"}); WriteResult({ "nInserted" : 1 }) > db.demo615.insert({"Value":300}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo615.find();This will produce the following output −{ "_id" : ObjectId("5e99bb3465492f6c60d0027f"), "Value" : 100 } { "_id" : ObjectId("5e99bb3865492f6c60d00280"), "Value" : "100" } { "_id" : ObjectId("5e99bb3c65492f6c60d00281"), "Value" : "300" } { "_id" : ObjectId("5e99bb4265492f6c60d00282"), "Value" ... Read More

What is ({$natural: 1}) in MongoDB?

AmitDiwan
Updated on 12-May-2020 06:40:05

721 Views

The ({$natural − 1}) works like LIFO(LAST IN FIRST OUT), that means last inserted document will be shown first.Let us create a collection with documents −> db.demo614.insertOne({"CountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cddf6b89257f5584d8e") } > db.demo614.insertOne({"CountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce0f6b89257f5584d8f") } > db.demo614.insertOne({"CountryName":"AUS"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988ce3f6b89257f5584d90") } > db.demo614.insertOne({"CountryName":"IND"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e988cebf6b89257f5584d91") }Display all documents from a collection with the help of find() method −> db.demo614.find();This will produce the following output −{ "_id" : ObjectId("5e988cddf6b89257f5584d8e"), "CountryName" : "US" } { ... Read More

Implementing String Comparison in MongoDB?

AmitDiwan
Updated on 11-May-2020 10:05:23

572 Views

To implement string comparison in MongoDB, use $strcasecmp. It performs case-insensitive comparison of two strings. It returns −1 if first string is “greater than” the second string.0 if the two strings are equal.-1 if the first string is “less than” the second string.Let us create a collection with documents −> db.demo490.insertOne({"Name1":"John", "Name2":"john"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496ccb0f3fa88e22790bb") } > db.demo490.insertOne({"Name1":"David", "Name2":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496d9b0f3fa88e22790bc") } > db.demo490.insertOne({"Name1":"Carol", "Name2":"Carol"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8496e5b0f3fa88e22790bd") }Display all documents from a collection with the help of find() method −> db.demo490.find();This will produce ... Read More

MongoDB query to update array object in index N?

AmitDiwan
Updated on 11-May-2020 10:04:05

219 Views

Use update() in MongoDB to update array object. The usage of dot notation is also required. Let us create a collection with documents −> db.demo489.insertOne( ... { ... ... ...    details : [{ ...       id : 101, ...       "Info1" : { ...          "StudentName" : "Chris" ...       }, ...       "Info2" : { ...          "TeacherName" : "David" ...       } ...    }, ...    { ...       id : 102, ...       "Info1" : ... Read More

Advertisements