AmitDiwan has Published 10744 Articles

MongoDB query to update a MongoDB row with only the objectid

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:43:57

202 Views

Use UPDATE to update and SET to set the new values. Let us create a collection with documents −> db.demo34.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1758b4cfb11e5c34d898cd") } > db.demo34.insertOne({"StudentFirstName":"David", "StudentAge":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e1758bdcfb11e5c34d898ce") } > db.demo34.insertOne({"StudentFirstName":"Bob", "StudentAge":20}); {    "acknowledged" ... Read More

MongoDB aggregation framework to sort by length of array?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:42:50

2K+ Views

To sort by length of array, use aggregate(). Before that, get the count of records in an arrayusing $sum. Let us create a collection with documents> db.demo33.insertOne({"ListOfStudent":["Chris", "Bob"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17556ccfb11e5c34d898ca") } > db.demo33.insertOne({"ListOfStudent":["David", "Adam", "Mike"]}); {    "acknowledged" : true,    "insertedId" : ... Read More

Looping MongoDB collection for sorting

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:41:13

123 Views

To sort records in a collection, use sort() in MongoDB. Let us create a collection with documents −> db.demo32.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e175158cfb11e5c34d898c5") } > db.demo32.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e17515ccfb11e5c34d898c6") } > db.demo32.insertOne({"Name":"Adam"}); {    "acknowledged" : true,    "insertedId" ... Read More

How to force MongoDB to use the BasicCursor instead of an index?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:39:38

188 Views

To avoid using index, use hint() in MongoDB. Let us create a collection with documents −> db.demo31.createIndex({"StudentFirstName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo31.insertOne({"StudentFirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174f8fcfb11e5c34d898c1") } > db.demo31.insertOne({"StudentFirstName":"Jace"}); { ... Read More

Get array items inside a MongoDB document?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:38:29

258 Views

To get array items in a MongoDB document, use the dot(.) notation. Let us create a collection with documents −> db.demo29.insertOne({"StudentDetails":[{"StudentName":"Chris", "StudentMarks":58}, {"StudentName":"Bob", "StudentMarks":69}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15fcc08f2315c2efc48e6e") } >db.demo29.insertOne({"StudentDetails":[{"StudentName":"David", "StudentMarks":97}, {"StudentName":"Carol", "StudentMarks":96}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15fcd38f2315c2efc48e6f") }Display all documents ... Read More

MongoDB query to find a specific city record from a collection

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:36:07

379 Views

For this, use find() and fetch a specific record. Let us create a collection with documents −> db.demo30.insertOne({"City":"New York"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174ceccfb11e5c34d898bd") } > db.demo30.insertOne({"City":"Los Angeles"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e174d0ecfb11e5c34d898be") } > db.demo30.insertOne({"City":"Chicago"}); {    "acknowledged" : true,   ... Read More

Can we use the “.” symbol in MongoDB collection name?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:30:08

252 Views

Yes, we can use the “.” symbol in MongoDB collection name. Let us create a collection with documents −> db.getCollection('demo28.example'); web.demo28.example > > > db.getCollection('demo28.example').insertOne({"Name":"Chris", "Age":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15fbe48f2315c2efc48e6b") } > db.getCollection('demo28.example').insertOne({"Name":"Bob", "Age":31}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e15fbec8f2315c2efc48e6c") } > ... Read More

Query in MongoDB to perform an operation similar to LIKE operation

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:23:37

175 Views

For similar operation, you can use / searchLetter /. Let us first create a collection with documents −> db.demo26.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c9dc22d07d3b95082e79") } > db.demo26.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c9e022d07d3b95082e7a") } > db.demo26.insertOne({"StudentName":"Jones"}); {    "acknowledged" : true,    "insertedId" ... Read More

Querying from part of object in an array with MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:22:11

178 Views

To query from part of object in an array, use $findOne() and $all. Let us first create a collection with documents −> db.demo25.insertOne( ... { ... ...    "Details":[ ...       { ...          "UserId":"Carol101", ...          "UserName":"Carol" ...       ... Read More

MongoDB query to push a computed expression in a $group?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:19:13

271 Views

To push a computed expression in $group, use aggregate. Let us first create a collection with documents −> db.demo24.insertOne({"Id":100, "Status":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c58722d07d3b95082e72") } > db.demo24.insertOne({"Id":100, "Status":true}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e14c58a22d07d3b95082e73") } > db.demo24.insertOne({"Id":100, "Status":false}); {    "acknowledged" : ... Read More

Advertisements