Found 1349 Articles for MongoDB

How to calculate sum in MongoDB with aggregate()?

AmitDiwan
Updated on 02-Apr-2020 06:58:31

2K+ Views

To get the sum, use $sum along with aggregate(). Let us create a collection with documents −> db.demo337.insertOne({"Amount":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231e5f8647eb59e56209b") } > db.demo337.insertOne({"Amount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231e9f8647eb59e56209c") } > db.demo337.insertOne({"Amount":400}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5231ebf8647eb59e56209d") }Display all documents from a collection with the help of find() method −> db.demo337.find();This will produce the following output −{ "_id" : ObjectId("5e5231e5f8647eb59e56209b"), "Amount" : 100 } { "_id" : ObjectId("5e5231e9f8647eb59e56209c"), "Amount" : 500 } { "_id" : ObjectId("5e5231ebf8647eb59e56209d"), "Amount" : 400 }Following is the query to calculate sum ... Read More

How to update only one property in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 06:56:15

368 Views

To update only one property, use $addToSet in MongoDB. Let us create a collection with documents −> db.demo336.insertOne({"Name":"Chris", "Score":[45, 67, 78]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522cb1f8647eb59e562097") } > db.demo336.insertOne({"Name":"David", "Score":[89, 93, 47]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522cb2f8647eb59e562098") }Display all documents from a collection with the help of find() method −> db.demo336.find();This will produce the following output −{ "_id" : ObjectId("5e522cb1f8647eb59e562097"), "Name" : "Chris", "Score" : [ 45, 67, 78 ] } { "_id" : ObjectId("5e522cb2f8647eb59e562098"), "Name" : "David", "Score" : [ 89, 93, 47 ] }Following is the query to update only ... Read More

MongoDB query to convert a string with commas to double

AmitDiwan
Updated on 02-Apr-2020 06:54:33

646 Views

For such conversion, use aggregate(). Let us create a collection with documents −> db.demo335.insertOne({"Value":"45, 67, 78.0"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522a1cf8647eb59e562091") } > db.demo335.insertOne({"Value":"17664, 76, 534.0"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522a26f8647eb59e562092") } > db.demo335.insertOne({"Value":"8899, 322, 135, 875.50"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522a34f8647eb59e562093") } > db.demo335.insertOne({"Value":"1, 533.07"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e522ab9f8647eb59e562094") }Display all documents from a collection with the help of find() method −> db.demo335.find();This will produce the following output −{ "_id" : ObjectId("5e522a1cf8647eb59e562091"), "Value" : "45, 67, 78.0" } { "_id" : ObjectId("5e522a26f8647eb59e562092"), ... Read More

MongoDB query to add matched key to list after query?

AmitDiwan
Updated on 02-Apr-2020 06:51:18

74 Views

For this, use aggregate() in MongoDB. Let us create a collection with documents −> db.demo334.insertOne({ ...    "Name": "Chris", ...    "Age": 21, ...    "details": [{ ...       "Subject": "MySQL", ...       "Score": 78 ...    }, { ...       "Subject": "MongoDB", ...       "Score": 45 ...    }] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e52241bf8647eb59e562090") }Display all documents from a collection with the help of find() method −> db.demo334.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e52241bf8647eb59e562090"),    "Name" : "Chris",   ... Read More

Count number of documents from MongoDB collection inside Array?

AmitDiwan
Updated on 02-Apr-2020 06:47:55

112 Views

To count the number of documents from the collection inside the array, use aggregate(). Let us create a collection with documents −> db.demo332.insertOne({details:[{Name:"Chris", Age:21}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e521f16f8647eb59e56208e") } > db.demo332.insertOne({details:[{Name:"David", Age:23}, {Name:"Bob", Age:22}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e521f21f8647eb59e56208f") }Display all documents from a collection with the help of find() method −> db.demo332.find();This will produce the following output −{ "_id" : ObjectId("5e521f16f8647eb59e56208e"), "details" : [ { "Name" : "Chris", "Age" : 21 } ] } { "_id" : ObjectId("5e521f21f8647eb59e56208f"), "details" : [ { "Name" : "David", "Age" : 23 }, { ... Read More

Create an index for text search in MongoDB

AmitDiwan
Updated on 02-Apr-2020 06:45:41

127 Views

Let us create a collection with documents −> db.demo331.insertOne({"Words":"This is a MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e521c35f8647eb59e562089") } > db.demo331.insertOne({"Words":"THIS is a MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e521c36f8647eb59e56208a") }Display all documents from a collection with the help of find() method−> db.demo331.find();This will produce the following output −{ "_id" : ObjectId("5e521c35f8647eb59e562089"), "Words" : "This is a MySQL" } { "_id" : ObjectId("5e521c36f8647eb59e56208a"), "Words" : "THIS is a MongoDB" }Following is the query to create an index for text search −> db.demo331.createIndex( {Words: "text" } ); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : ... Read More

Restrict returned data in MongoDB to display only specific values from documents

AmitDiwan
Updated on 02-Apr-2020 06:42:44

313 Views

To restrict returned data, use find(). The values 0 and 1 for fields would decide what all field values would be visible or hidden.Let us create a collection with documents −> db.demo330.insertOne({"Id":101, "Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e52149ff8647eb59e562081") } > db.demo330.insertOne({"Id":102, "Name":"Sam", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214aaf8647eb59e562082") } > db.demo330.insertOne({"Id":103, "Name":"David", "Age":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214b3f8647eb59e562083") } > db.demo330.insertOne({"Id":104, "Name":"Bob", "Age":23}); . {    "acknowledged" : true,    "insertedId" : ObjectId("5e5214bdf8647eb59e562084") }Display all documents from a collection with the help of find() method −> db.demo330.find();This ... Read More

MongoDB query to sum specific key values with terminal commands?

AmitDiwan
Updated on 02-Apr-2020 06:41:30

122 Views

For this, use $match along with aggregate(). Let us create a collection with documents −> db.demo329.insertOne({"Name":"Chris", "Age":21, "Marks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516f28f8647eb59e56207d") } > db.demo329.insertOne({"Name":"David", "Age":21, "Marks":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516f28f8647eb59e56207e") } > db.demo329.insertOne({"Name":"Mike", "Age":21, "Marks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516f29f8647eb59e56207f") } > db.demo329.insertOne({"Name":"David", "Age":21, "Marks":89}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516f29f8647eb59e562080") }Display all documents from a collection with the help of find() method −> db.demo329.find().pretty();This will produce the following output:{    "_id" : ObjectId("5e516f28f8647eb59e56207d"),    "Name" : "Chris",    "Age" : 21, ... Read More

Using findOneAndUpdate () to update in MongoDB?

AmitDiwan
Updated on 02-Apr-2020 06:36:56

231 Views

The findOneAndUpdate() is used to update a single document based on the filter and sort criteria i.e. −db.collection.findOneAndUpdate(filter, update, options)Let us create a collection with documents −> db.demo328.insertOne({Name:"Chris", Marks:67}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b19f8647eb59e56207a") } > db.demo328.insertOne({Name:"David", Marks:78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b24f8647eb59e56207b") } > db.demo328.insertOne({Name:"Bob", Marks:97}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516b2bf8647eb59e56207c") }Display all documents from a collection with the help of find() method −> db.demo328.find();This will produce the following output −{ "_id" : ObjectId("5e516b19f8647eb59e56207a"), "Name" : "Chris", "Marks" : 67 } { "_id" : ObjectId("5e516b24f8647eb59e56207b"), "Name" : ... Read More

MongoDB query to set user defined variable into query?

AmitDiwan
Updated on 02-Apr-2020 06:35:34

363 Views

For user-defined variables, use var keyword in MongoDB. Let us create a collection with documents −> db.demo327.insertOne({"FirstName":"Chris", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516952f8647eb59e562076") } > db.demo327.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e51695af8647eb59e562077") } > db.demo327.insertOne({"FirstName":"John", "LastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516962f8647eb59e562078") } > db.demo327.insertOne({"FirstName":"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e516968f8647eb59e562079") }Display all documents from a collection with the help of find() method −> db.demo327.find();This will produce the following output −{ "_id" : ObjectId("5e516952f8647eb59e562076"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e51695af8647eb59e562077"), ... Read More

Advertisements