Found 1349 Articles for MongoDB

How do I delete array value from a document in MongoDB?

AmitDiwan
Updated on 14-May-2020 06:33:19

388 Views

To delete array values, use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.Let us first create a collection with documents −> db.demo535.insertOne( ... { ... ...    "studentId" : "101", ...    "studentName" : "Chris", ...    "ListOfMailIds" : [ ...       "Chris@gmail.com", ...       "Chris@yahoo.com" ...    ] ... ... } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e8c82bfef4dcbee04fbbc00") }Display all documents from a collection with the help of find() method −> db.demo535.find();This will produce the following ... Read More

“Structured” grouping query in MongoDB to display result with a new field displaying the count

AmitDiwan
Updated on 14-May-2020 06:29:48

86 Views

For this, use $group in MongoDB IN aggregate(). The $group groups input documents by the specified _id expression and for each distinct grouping, outputs a document. Let us first create a collection with documents −> db.demo534.insertOne({_id:10, "ProductId":100, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 10 } > db.demo534.insertOne({_id:11, "ProductId":100, "ProductName":"Product-2"}); { "acknowledged" : true, "insertedId" : 11 } > db.demo534.insertOne({_id:12, "ProductId":101, "ProductName":"Product-1"}); { "acknowledged" : true, "insertedId" : 12 }Display all documents from a collection with the help of find() method −> db.demo534.find();This will produce the following output −{ "_id" : 10, "ProductId" : 100, "ProductName" : "Product-1" } { ... Read More

Unable to implement $addToSet in MongoDB to fetch values of a single field?

AmitDiwan
Updated on 14-May-2020 06:25:20

208 Views

The $addToSet operator adds value to an array unless the value is already present, in which case $addToSet does nothing to that array.Let us create a collection with documents −> db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4cfaef4dcbee04fbbbfc") } > db.demo533.insertOne({"ProjectName":"Online Library Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d02ef4dcbee04fbbbfd") } > db.demo533.insertOne({"ProjectName":"Online Hospital Management"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d04ef4dcbee04fbbbfe") } > db.demo533.insertOne({"ProjectName":"Online Customer Tracker"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4d0def4dcbee04fbbbff") }Display all documents from a collection with the help of find() method −> db.demo533.find();This will produce the following output −{ ... Read More

MongoDB query to fetch only the “Name” field based on roles?

AmitDiwan
Updated on 14-May-2020 06:22:55

212 Views

For this, use aggregate(). Here, we have considered 3 roles − Admin, Guest, and User. Let us create a collection with documents −> db.demo532.insertOne({"Name":"Chris", "Type":"Admin"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4a9def4dcbee04fbbbf9") } > db.demo532.insertOne({"Name":"David", "Type":"Guest"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa") } > db.demo532.insertOne({"Name":"Bob", "Type":"User"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8b4ab0ef4dcbee04fbbbfb") }Display all documents from a collection with the help of find() method −> db.demo532.find();This will produce the following output −{ "_id" : ObjectId("5e8b4a9def4dcbee04fbbbf9"), "Name" : "Chris", "Type" : "Admin" } { "_id" : ObjectId("5e8b4aa3ef4dcbee04fbbbfa"), "Name" : "David", "Type" : "Guest" } { "_id" ... Read More

How I can use a database-name with special characters like " customer_tracker-990" in MongoDB console

AmitDiwan
Updated on 13-May-2020 10:25:43

339 Views

Yes, use getSiblingDB(). Let us add some documents to the database −> use customer_tracker-990; switched to db customer_tracker-990 > db.demo1.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4697ca7e81adc6a0b3954") } > db.demo1.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46980a7e81adc6a0b3955") } > db.demo1.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea46984a7e81adc6a0b3956") }Display all documents from a collection with the help of find() method −> db.getSiblingDB("customer_tracker-990").demo1.find();This will produce the following output −{ "_id" : ObjectId("5ea4697ca7e81adc6a0b3954"), "Name" : "Chris" } { "_id" : ObjectId("5ea46980a7e81adc6a0b3955"), "Name" : "David" } { "_id" : ObjectId("5ea46984a7e81adc6a0b3956"), "Name" : "Bob" }Read More

Limit the number of documents in a collection in MongoDB?

AmitDiwan
Updated on 13-May-2020 10:24:09

372 Views

To limit the number of documents in a collection, set capped − true. Set the size there itself. Let us create a collection with documents −> db.createCollection( "demo683", { capped: true, size: 5 ,max:4} ) { "ok" : 1 } > db.demo683.insertOne({Value:100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468afa7e81adc6a0b394e") } > db.demo683.insertOne({Value:500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b0a7e81adc6a0b394f") } > db.demo683.insertOne({Value:1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b1a7e81adc6a0b3950") } > db.demo683.insertOne({Value:400}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b2a7e81adc6a0b3951") } > db.demo683.insertOne({Value:800}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea468b3a7e81adc6a0b3952") } ... Read More

Is there any way to skip some documents in MongoDB?

AmitDiwan
Updated on 13-May-2020 10:22:17

102 Views

Yes, you can skip some documents using skip() in MongoDB. Use limit() to display how many documents you want to display after skipping some. Let us create a collection with documents −> db.demo682.insertOne({FirstName:"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462a804263e90dac94402") } > db.demo682.insertOne({FirstName:"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ac04263e90dac94403") } > db.demo682.insertOne({FirstName:"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462af04263e90dac94404") } > db.demo682.insertOne({FirstName:"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462b304263e90dac94405") } > db.demo682.insertOne({FirstName:"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea462ba04263e90dac94406") } > db.demo682.insertOne({FirstName:"Chris"}); {    "acknowledged" : true,    "insertedId" ... Read More

Get the top most document from a MongoDB collection

AmitDiwan
Updated on 13-May-2020 10:19:35

131 Views

To get the topmost document, use find() along with limit(). To fetch only a single document, consider using limit(1). Let us create a collection with documents −> db.demo681.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo681.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo681.insertOne({_id:103, Name:"David"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo681.insertOne({_id:104, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 104 } > db.demo681.insertOne({_id:105, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 105 }Display all documents from a collection with the help of find() method −> db.demo681.find();This will produce the following output −{ ... Read More

Set $gt condition in MongoDB $and

AmitDiwan
Updated on 13-May-2020 10:18:33

176 Views

The $and performs a logical AND operation on an array of one or more expressions. Let us create a collection with documents −> db.demo680.insertOne({Values:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461b04263e90dac943fe") } > db.demo680.insertOne({Values:70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4461e04263e90dac943ff") } > db.demo680.insertOne({Values:[80, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4462a04263e90dac94400") } > db.demo680.insertOne({Values:20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea4463304263e90dac94401") }Display all documents from a collection with the help of find() method −> db.demo680.find();This will produce the following output −{ "_id" : ObjectId("5ea4461b04263e90dac943fe"), "Values" : 40 } { "_id" : ... Read More

Pull an element in sub of sub-array in MongoDB?

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

343 Views

To pull an element, use $pull along with $(positional) operator. Let us create a collection with documents −> db.demo679.insertOne( ...    { ...       id:1, ...       "details": [ ...          { ...             CountryName:"US", ...             "information": [ ... ...                { "Name": "Chris", "FirstName": "Name=Chris" }, ... ...                {"Name": "Bob", "FirstName": "Name=Bob" } ...             ] ...          }, ... ... Read More

Advertisements