AmitDiwan has Published 10744 Articles

MongoDB query to convert from ObjectId to String

AmitDiwan

AmitDiwan

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

2K+ 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" : ... Read More

Set a similar name from another column in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:30:22

171 Views

Simply loop with forEach() and set column value from another column. Let us create a collection with documents −> db.demo51.insert({"Name1":"Chris", "Name":"David", "Age":24}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Carol", "Name":"Mike", "Age":22}); WriteResult({ "nInserted" : 1 }) > db.demo51.insert({"Name1":"Sam", "Name":"Bob", "Age":26}); WriteResult({ "nInserted" : 1 })Display all documents from a collection ... Read More

How do I get a value array (instead a json array) greater than 50 in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:29:00

119 Views

To avoid getting json array and get a value array, use $in. For greater than, use MongoDB $gt. Let us create a collection with documents −> db.demo50.save({"Value":40}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":100}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":20}); WriteResult({ "nInserted" : 1 }) > db.demo50.save({"Value":510}); WriteResult({ "nInserted" ... Read More

Searching for an array entry via its id in a MongoDB collection and performing update

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:27:44

109 Views

To search for an array via id, use positional $ operator. For update, use the UPDATE in MongoDB. Let us create a collection with documents −> db.demo49.insertOne( ... { ... ...    "Name": "David", ...    "Details": [ ...       { ...          "_id": "D1234", ... Read More

MongoDB query to get a specific number of items

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:25:41

280 Views

To get a specific number of items, use $slice operator in MongoDB. Let us create a collection with documents −> db.demo48.insertOne({"Name":["David", "Chris", "Sam", "Mike", "Carol"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e270491cfb11e5c34d89901") }Display all documents from a collection with the help of find() method −> db.demo48.find();This will produce ... Read More

How to return the position of a document relative to the collection in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:24:16

574 Views

To return the position of a document relative to the collection, use sort() along with count(). Let us create a collection with documents −> db.demo47.insertOne({"ClientName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267240cfb11e5c34d898f0") } > db.demo47.insertOne({"ClientName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e267243cfb11e5c34d898f1") } > db.demo47.insertOne({"ClientName":"Chris"}); { ... Read More

Indexing large text field to make query faster in MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:22:18

211 Views

To index large text field, use ensureIndex() along with $regex for text search. Let us create a collection with documents −> db.demo46.ensureIndex({"Name":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo46.insertOne({"Name":"John Smith"}); {    "acknowledged" : true,    "insertedId" ... Read More

How to get a saved object in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:21:09

262 Views

Let us first create a variable. Following is the query −> var studentDetails={"StudentFirstName":"Chris", "StudentLastName":"Brown", "StudentAge":24};Following is the query to save records using save() −> db.demo45.save(studentDetails); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> studentDetails;This will produce the following output −{   ... Read More

How to store query output in temp MongoDB database?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:19:23

753 Views

For this, in a single query, simply work with forEach() and store output in a temp db. Let us first create a collection with documents −> db.demo43.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25d4b3cfb11e5c34d898e5") } > db.demo43.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e25d4b8cfb11e5c34d898e6") } > ... Read More

Rebuilding indexes in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 12:18:08

626 Views

To rebuild indexes, use reIndex(). Let us first create an index. Following is the query −> db.demo42.createIndex({"StudentFirstName":1});This will produce the following output −{    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Following is the query to rebuild index in MongoDB −> db.demo42.reIndex({"StudentFirstName":1});This ... Read More

Advertisements