AmitDiwan has Published 10744 Articles

MongoDB query to get documents with multiple conditions set in $or?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 10:03:43

2K+ Views

Let us create a collection with documents −> db.demo711.insertOne({Name:"John", "Marks":75, Age:21, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c215d33e20ed1097b7e") } > db.demo711.insertOne({Name:"Chris", "Marks":55, Age:22, status:"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea85c2c5d33e20ed1097b7f") } > db.demo711.insertOne({Name:"Bob", "Marks":45, Age:20, status:"Inactive"}); {    "acknowledged" : true,    "insertedId" : ... Read More

Sort id and reverse the items with MongoDB

AmitDiwan

AmitDiwan

Updated on 14-May-2020 10:01:05

1K+ Views

The $natural returns the documents in natural order. To reverse the items, use $natural:-1. Let us create a collection with documents −> db.demo710.insertOne({id:101, Name:"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a855d33e20ed1097b7a") } > db.demo710.insertOne({id:102, Name:"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea83a8d5d33e20ed1097b7b") } > db.demo710.insertOne({id:103, Name:"Mike"}); ... Read More

MongoDB aggregation to get two documents with the least marks

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:58:33

160 Views

To get the sorted list of marks, use $sort. Use $limit: 2 to display only two such documents with least marks. Let us create a collection with documents −> db.demo709.insertOne({Name:"John", "Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea839005d33e20ed1097b76") } > db.demo709.insertOne({Name:"Chris", "Marks":45}); {    "acknowledged" : true,   ... Read More

Find MongoDB records based on a condition?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:56:29

1K+ Views

To find MongoDB based on a condition, use find() and set the condition. Let us create a collection with documents −> db.demo708.insertOne({"Name":"John", Marks:54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702e4d346dcb074dc6f33") } > db.demo708.insertOne({"Name":"Chris", Marks:35}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea702e6d346dcb074dc6f34") } > db.demo708.insertOne({"Name":"David", Marks:45}); { ... Read More

Set server status to inactive in a MongoDB collection with server records?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:55:33

345 Views

Let us create a collection with documents −> db.demo707.insertOne( ...    { ...       id:101, ...       "serverInformation": ...       [ ...          { ...             "IP":"192.56.34.3", ...             "Status":"Active" ... ... Read More

Display MongoDB with document and subdocument example and update

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:46:18

319 Views

Following is the syntax showing document and subdocument −db.yourCollectionName.insertOne(    {       yourFiledName:yourValue,       yourFieldName : [          {             yourFiledName1,             yourFiledName2,             .       ... Read More

MongoDB - Query embedded documents?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:43:40

342 Views

To query embedded documents in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo705.insertOne( ...    { ...       _id:101, ...       "Information": ...       [ ...          { ...             "StudentName":"Chris", ... ... Read More

How to get unique values from MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:41:17

17K+ Views

To get unique values and ignore duplicates, use distinct() in MongoDB. The distinct() finds the distinct values for a specified field across a single collection and returns the results in an array.Let us create a collection with documents −> db.demo704.insertOne({"LanguageCode":"hi"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ee18551299a9f98c93bd") } ... Read More

MongoDB query to count the number of array items in documents and display in a new field

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:39:38

145 Views

To count the number of array items in a document, use $size in MongoDB. Let us create a collection with documents −> db.demo703.insertOne({"ListOfSubject":["MySQL", "MongoDB"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ebaf551299a9f98c93b4") } > db.demo703.insertOne({"ListOfSubject":["Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6ebb5551299a9f98c93b5") } > db.demo703.insertOne({"ListOfSubject":["C", "C++", "Python"]}); ... Read More

Create index in a MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:39:12

217 Views

To create index, use createIndex() in MongoDB. Let us create a collection with documents −> db.demo702.createIndex({"details.id":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo702.insertOne({ ...    "details" : [ ...       { ...       ... Read More

Advertisements