Found 1349 Articles for MongoDB

How to query MongoDB with a LIMIT?

AmitDiwan
Updated on 03-Apr-2020 12:41:18

130 Views

To query MongoDB with limit, use LIMIT() method. Let us create a collection with documents −> db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f8fcfb11e5c34d8991f") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f93cfb11e5c34d89920") } > db.demo58.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f94cfb11e5c34d89921") } > db.demo58.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285f99cfb11e5c34d89922") }Display all documents from a collection with the help of find() method −> db.demo58.find();This will produce the following output −{ "_id" : ObjectId("5e285f8fcfb11e5c34d8991f"), "Name" : "David" } { "_id" : ObjectId("5e285f93cfb11e5c34d89920"), "Name" : "David" } { "_id" : ... Read More

MongoDB query to update only a single item from voting (up and down) records?

AmitDiwan
Updated on 03-Apr-2020 12:39:49

75 Views

Let us first create a collection with documents −> db.demo57.insertOne({"Votes":{"VoterName":"Chris", "TotalVote":50}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bb8cfb11e5c34d8991a") } > db.demo57.insertOne({"Votes":{"VoterName":"David", "TotalVote":101}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e285bc3cfb11e5c34d8991b") }Display all documents from a collection with the help of find() method −> db.demo57.find();This will produce the following output −{ "_id" : ObjectId("5e285bb8cfb11e5c34d8991a"), "Votes" : { "VoterName" : "Chris", "TotalVote" : 50 } } { "_id" : ObjectId("5e285bc3cfb11e5c34d8991b"), "Votes" : { "VoterName" : "David", "TotalVote" : 101 } }Here is the query to update only a single item (TotalVote) −> db.demo57.update({"Votes.VoterName":"David" }, { $inc : { "Votes.TotalVote" ... Read More

How to efficiently run complex queries on MongoDB unindexed fields?

AmitDiwan
Updated on 03-Apr-2020 12:40:09

205 Views

Create an index to efficiently run complex queries. Let us first create a collection with documents −> db.demo400.insertOne({SubjectName:"Java Spring"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610720fac4d418a0178572") } > db.demo400.insertOne({SubjectName:"Spring Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61072dfac4d418a0178573") } > db.demo400.insertOne({SubjectName:"Java Hibernate"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610736fac4d418a0178574") } > db.demo400.createIndex({SubjectName:"text"}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Display all documents from a collection with the help of find() method −> db.demo400.find();This will produce the following output −{ "_id" : ObjectId("5e610720fac4d418a0178572"), "SubjectName" : "Java ... Read More

MongoDB query to remove a specific document

AmitDiwan
Updated on 03-Apr-2020 12:39:02

131 Views

To remove a specific document, use remove() in MongoDB. Let us create a collection with documents −> db.demo56.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e0bcfb11e5c34d89917") } > db.demo56.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e10cfb11e5c34d89918") } > db.demo56.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e272e13cfb11e5c34d89919") }Display all documents from a collection with the help of find() method −> db.demo56.find();This will produce the following output −{ "_id" : ObjectId("5e272e0bcfb11e5c34d89917"), "Name" : "Chris" } { "_id" : ObjectId("5e272e10cfb11e5c34d89918"), "Name" : "David" } { "_id" : ObjectId("5e272e13cfb11e5c34d89919"), "Name" : "Bob" }Following is the query to remove a ... Read More

How to push an array in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:38:25

411 Views

To push an array, use $push in MongoDB. Let us first create a collection with documents −> db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610339fac4d418a017856d") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610341fac4d418a017856e") } > db.demo399.insertOne({Name:"Chris", Age:21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610355fac4d418a017856f") } > db.demo399.insertOne({Name:"Bob", Age:23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e61035efac4d418a0178570") } > db.demo399.insertOne({Name:"David", Age:22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e610364fac4d418a0178571") }Display all documents from a collection with the help of find() method −> db.demo399.find();This will produce the following output −{ ... Read More

How to copy attributes in MongoDB?

AmitDiwan
Updated on 03-Apr-2020 12:37:39

431 Views

To copy the value of one attribute to another, use $set along with update(). Let us create a collection with documents −> db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2019-01-21")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716dfcfb11e5c34d89915") } > db.demo55.insertOne({"ShippingDate":'', "date":new ISODate("2020-05-12")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e2716ebcfb11e5c34d89916") }Display all documents from a collection with the help of find() method −> db.demo55.find();This will produce the following output −{ "_id" : ObjectId("5e2716dfcfb11e5c34d89915"), "ShippingDate" : "", "date" : ISODate("2019-01-21T00:00:00Z") } { "_id" : ObjectId("5e2716ebcfb11e5c34d89916"), "ShippingDate" : "", "date" : ISODate("2020-05-12T00:00:00Z") }Following is the query to copy attributes in MongoDB −> db.demo55.find({}).forEach(function(c){ ... ... Read More

Set MongoDB $slice with a range?

AmitDiwan
Updated on 03-Apr-2020 12:35:28

192 Views

To set slice along with a range, use the $slice operator with parameters. These parameters are to be set for beginning position of the elements to be fetched and the 2nd parameter is for range. Let us create a collection with documents −> db.demo54.insertOne({"ListOfValues":[100, 2030, 5353, 7364, 635, 535, 524, 423, 2434, 1323, 799874, 90]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27151ecfb11e5c34d89914") }Display all documents from a collection with the help of find() method −> db.demo54.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e27151ecfb11e5c34d89914"),    "ListOfValues" : [       100,       2030, ... Read More

MongoDB query to convert an array to a map of documents with n attributes?

AmitDiwan
Updated on 03-Apr-2020 12:36:17

689 Views

For this, you can use $map. Let us first create a collection with documents −> db.demo398.insertOne({ ...    "details":[ ...       { ...          "Name":"Chris", ...          "Age":22 ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e8cedfac4d418a017856c") }Display all documents from a collection with the help of find() method −> db.demo398.find();This will produce the following output −{ "_id" : ObjectId("5e5e8cedfac4d418a017856c"), "details" : [ { "Name" : "Chris", "Age" : 22 } ] }Following is the query to convert an array ... Read More

How do I index “or” in MongoDB for indexing multiple fields?

AmitDiwan
Updated on 03-Apr-2020 12:33:44

219 Views

To index multiple fields, use ensureIndex() for a combination. With ensureIndex(), we can create an index and even pass multiple fields. Let us create a collection with documents −> db.demo53.ensureIndex({"StudentFirstName":1, "StudentAge":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo53.ensureIndex({"StudentFirstName":1, "StudentCountryName":1}); {    "createdCollectionAutomatically" : false,    "numIndexesBefore" : 2,    "numIndexesAfter" : 3,    "ok" : 1 } >db.demo53.insertOne({"StudentFirstName":"Chris", "StudentAge":21, "StudentCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e271431cfb11e5c34d89911") } >db.demo53.insertOne({"StudentFirstName":"David", "StudentAge":23, "StudentCountryName":"UK"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27143ccfb11e5c34d89912") } >db.demo53.insertOne({"StudentFirstName":"Mike", "StudentAge":24, "StudentCountryName":"AUS"}); { ... Read More

MongoDB query to convert from ObjectId to String

AmitDiwan
Updated on 03-Apr-2020 12:31:37

1K+ Views

To convert from ObjectId to String, use toString() in MongoDB. Let us create a collection with documents −> db.demo52.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e27129bcfb11e5c34d89910") }Display all documents from a collection with the help of find() method −> db.demo52.find();This will produce the following output −{ "_id" : ObjectId("5e27129bcfb11e5c34d89910"), "StudentName" : "Chris" }Following is the query to convert ObjectId to String −> ObjectId("5e27129bcfb11e5c34d89910").toString(); ObjectId("5e27129bcfb11e5c34d89910")Now you can check whether the ObjectId is in string or not −> typeof ObjectId("5e27129bcfb11e5c34d89910").toString();This will produce the following output −StringRead More

Advertisements