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
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
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 with the help of find() method −> db.demo51.find();This will produce the following output −{ "_id" : ObjectId("5e27108ccfb11e5c34d8990d"), "Name1" : "Chris", "Name" : "David", "Age" : 24 } { "_id" : ObjectId("5e27108dcfb11e5c34d8990e"), "Name1" : "Carol", "Name" : "Mike", "Age" : 22 } { "_id" : ObjectId("5e27108ecfb11e5c34d8990f"), "Name1" : "Sam", "Name" : ... Read More
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" : 1 })Display all documents from a collection with the help of find() method −> db.demo50.find();This will produce the following output −{ "_id" : ObjectId("5e270c02cfb11e5c34d89903"), "Value" : 40 } { "_id" : ObjectId("5e270c05cfb11e5c34d89904"), "Value" : 100 } { "_id" : ObjectId("5e270c07cfb11e5c34d89905"), "Value" : 20 } { "_id" : ObjectId("5e270c11cfb11e5c34d89906"), "Value" ... Read More
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", ... "Subject":"MySQL" ... }, ... { ... "_id": "E234", ... "Subject":"Java" ... }, ... { ... "_id": "F456", ... ... Read More
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 the following output −{ "_id" : ObjectId("5e270491cfb11e5c34d89901"), "Name" : [ "David", "Chris", "Sam", "Mike", "Carol" ] }Following is the query to get only 2 items −> db.demo48.find({}, {Name:{$slice: 2}});This will produce the following output −{ "_id" : ObjectId("5e270491cfb11e5c34d89901"), "Name" : [ "David", "Chris" ] }Read More
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"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267247cfb11e5c34d898f2") } > db.demo47.insertOne({"ClientName":"Sam"}); { "acknowledged" : true, "insertedId" : ObjectId("5e26724ccfb11e5c34d898f3") }Display all documents from a collection with the help of find() method −> db.demo47.find();This will produce the following output −{ "_id" : ObjectId("5e267240cfb11e5c34d898f0"), "ClientName" : "Adam" } { "_id" : ... Read More
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" : ObjectId("5e267004cfb11e5c34d898ed") } > db.demo46.insertOne({"Name":"John Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267009cfb11e5c34d898ee") } > db.demo46.insertOne({"Name":"Chris Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e267011cfb11e5c34d898ef") }Display all documents from a collection with the help of find() method −> db.demo46.find();This will produce the following output −{ "_id" : ... Read More
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 −{ "StudentFirstName" : "Chris", "StudentLastName" : "Brown", "StudentAge" : 24, "_id" : ObjectId("5e25dab4cfb11e5c34d898ec") }
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") } > db.demo43.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e25d4bbcfb11e5c34d898e7") }Display all documents from a collection with the help of find() method −> db.demo43.find();This will produce the following output −{ "_id" : ObjectId("5e25d4b3cfb11e5c34d898e5"), "StudentName" : "Chris" } { "_id" : ObjectId("5e25d4b8cfb11e5c34d898e6"), "StudentName" : "Bob" } { "_id" : ObjectId("5e25d4bbcfb11e5c34d898e7"), "StudentName" ... Read More