AmitDiwan has Published 10744 Articles

How can I find documents in MongoDB based on the number of matched objects within an array?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:20:21

147 Views

Let us see an example and create a collection with documents −> db.demo694.insertOne( ...    { ...       "details" : ...       [ ...          { ...             "Name" : "Chris", ...             ... Read More

Ignore first 4 values in MongoDB documents and display the next 3?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:16:27

128 Views

For this, use $slice and set thecount of values to be ignored and displayed. Let us create a collection with documents −> db.demo693.insertOne({Values:[10, 746, 736, 283, 7363, 424, 3535]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea58a04ece4e5779399c07b") } > db.demo693.insertOne({Values:[100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999]}); ... Read More

MongoDB query to find documents with specific FirstName and LastName

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:14:20

978 Views

To find documents with specific FirstName and LastName, use $and along with $in. Implement this in MongoDB find(). Let us create a collection with documents −> db.demo692.insertOne({FirstName:"Chris", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585dca7e81adc6a0b396a") } > db.demo692.insertOne({FirstName:"John", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585e2a7e81adc6a0b396b") ... Read More

MongoDB query to rename a collection?

AmitDiwan

AmitDiwan

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

163 Views

To rename a collection in MongoDB, use renameCollection(). Let us create a collection with documents −> db.demo690.insertOne({_id:101, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo690.insertOne({_id:102, Name:"Mike"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo690.insertOne({_id:103, Name:"John"}); { "acknowledged" : true, "insertedId" : 103 }Display all documents ... Read More

Writing a MongoDB insert statement for multiple insert at a time

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:10:04

235 Views

For multiple insert, use insert() in MongoDB. Let us create a collection with document −> db.demo689.insert([ ...    {ClientName:"Chris", "ClientAge":34, "ClientCountryName":"US"}, ...    {ClientName:"David", "ClientAge":28, "ClientCountryName":"UK"}, ...    {ClientName:"Bob", "ClientAge":39, "ClientCountryName":"AUS"}, ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" ... Read More

Set condition in MongoDB nested document?

AmitDiwan

AmitDiwan

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

326 Views

Let’s say we need to find a document with a value greater than specific value. For this, use dot notation in nested document and set the condition with $gt.Let us see an example and create a collection with documents −> db.demo688.insert( ... { ... information:{id:1, details:[ ...    {otherDetails:{ ... ... Read More

MongoDB aggregation to fetch documents with specific field value?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 09:03:57

656 Views

For this, use aggregate(). Let’s say we have to fetch documents with a field “Age” with value “21”.Let us now create a collection with documents −> db.demo685.insertOne( ...    { ...       "details": ...       [ ...          { ...       ... Read More

Accessing inner element of JSON array in MongoDB?

AmitDiwan

AmitDiwan

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

668 Views

To access inner element of JSON array in MongoDB, use dot notation. Let us create a collection with documents −> db.demo687.insert({CountryName:'US', ... info: ... { ... id:101, ... details: ... [ ... { ...    Name:'Chris', ...    SubjectName:'MongoDB', ...    otherDetails:{ ...       "Marks":58, ...     ... Read More

How to query MongoDB similar to “like” ?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:59:59

367 Views

To implement similar to “like”, use find() along with // in MongoDB. Let us create a collection with documents −> db.demo686.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea55182a7e81adc6a0b395c") } > db.demo686.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea55186a7e81adc6a0b395d") } > db.demo686.insertOne({"FirstName":"ROBERT"}); {    "acknowledged" : true, ... Read More

How do I sort natural in MongoDB?

AmitDiwan

AmitDiwan

Updated on 14-May-2020 08:52:35

841 Views

Use $natural to sort natural in MongoDB. Let us create a collection with documents −> db.demo684.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530cea7e81adc6a0b3957") } > db.demo684.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530d1a7e81adc6a0b3958") } > db.demo684.insertOne({Value:60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530d4a7e81adc6a0b3959") } ... Read More

Advertisements