AmitDiwan has Published 10744 Articles

Working with MongoDB $sort for sub array document

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 12:17:52

719 Views

For sub array document in MongoDB, use aggregate along with $sort. Let us first create a collection with documents −> db.demo23.insertOne( ...{ ... ...    "StudentDetails" : [{ ...       "Name" : "David", ...       "Age" : 23, ... ...    }, { ...     ... Read More

Sorting field value (FirstName) for MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:37:20

361 Views

To sort values, use sort() in MongoDB. Let us first create a collection with documents −> db.demo365.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5b6d0ada61456dc936f") } > db.demo365.insertOne({"FirstName":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bad0ada61456dc9370") } > db.demo365.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d5bed0ada61456dc9371") ... Read More

MongoDB query to find records with keys containing dots?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:35:48

808 Views

For this, use $addFields. In that, use the $objectToArray to get the data in the form of keys and values. Use $filter with $indexOfBytes to check if there are any keys with. inside it.Let us first create a collection with documents −> db.demo364.insertOne( ...   { ...       ... Read More

How to use deleteOne() function in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:31:01

283 Views

The deleteOne() function in MongoDB deletes at most one matching document from a collection. Let us first create a collection with documents −> db.demo363.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c3d0ada61456dc9369") } > db.demo363.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e57d2c7d0ada61456dc936a") } > db.demo363.insertOne({"Name":"Bob"}); {   ... Read More

Fetch multiple documents in MongoDB query with OR condition?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:26:29

174 Views

Let us create a collection with documents −> db.demo362.insertOne({"ClientName":"John", "ClientProject":"School Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a77454a481fef8ec7a1c") } > db.demo362.insertOne({"ClientName":"David", "ClientProject":"Library Management System"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a78454a481fef8ec7a1d") } > db.demo362.insertOne({"ClientName":"Mike", "ClientProject":"Event Tracker"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a7a054a481fef8ec7a1e") ... Read More

Using object as key for finding values in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:24:31

1K+ Views

For finding values, use find() in MongoDB. Under that, use dot notation. Let us create a collection with documents −> db.demo361.insertOne({"details":{"FirstName":"Chris", "LastName":"Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a61f54a481fef8ec7a19") } > db.demo361.insertOne({"details":{"FirstName":"David", "LastName":"Miller"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a62854a481fef8ec7a1a") } > db.demo361.insertOne({"details":{"FirstName":"John", "LastName":"Doe"}}); {   ... Read More

Find current and previous documents in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:22:14

659 Views

To get the current and previous documents, use sort(). Since we need only 2 documents, therefore, use LIMIT(2).Let us create a collection with documents −> db.demo360.insertOne({id:101, "Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a10c54a481fef8ec7a15") } > db.demo360.insertOne({id:102, "Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e56a11254a481fef8ec7a16") } ... Read More

MongoDB query to concatenate values of array with other fields

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:20:59

1K+ Views

To concatenate in MongoDB, use $concat in $project. Let us create a collection with documents −> db.demo359.insertOne( ...    { ... ...       Name1: "Chris", ...       Name2: "David", ...       Subjects: ["MySQL", "MongoDB", "Java"] ...    } ... ); {    "acknowledged" : ... Read More

Does aggregation query with $match works in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:18:18

159 Views

Yes, it works in MongoDB. Let us create a collection with documents −> db.demo358.insertOne({"ClientId":101, "ClientName":"Chris", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692c0f8647eb59e5620cb") } > db.demo358.insertOne({"ClientId":102, "ClientName":"David", "ClientAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5692caf8647eb59e5620cc") } > db.demo358.insertOne({"ClientId":103, "ClientName":"David", "ClientAge":35}); {    "acknowledged" : true,   ... Read More

MongoDB query to fetch a specific document rom documents with field value set using NumberInt()?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 08:15:55

280 Views

The NumberInt() is used to explicitly specify 32-bit integers. Let us create a collection with documents −> db.demo357.insertOne( ...    { ...       "FirstName" : "Chris", ...       "Age" : 21, ...       "details" : { ...          "studentDetails" : { ... Read More

Advertisements