AmitDiwan has Published 10744 Articles

MongoDB query for documents whose array elements does not have a specific value

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:27:43

520 Views

For such cases, use $elemMatch. This operator matches documents that contain an array field with at least one element that matches all the specified query criteria.Let us create a collection with documents −> db.demo239.insertOne( ...   { ...      "Name" : "Chris", ...      "details" : [ ... ... Read More

Implement MongoDB Aggregate - unwind, group and project?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:24:15

2K+ Views

The $unwind in MongoDB deconstructs an array field from the input documents to output a document for each element.$group is used to group input documents by the specified _id expression and for each distinct grouping, outputs a document.$project is used to pass along the documents with the requested fields to ... Read More

Remove all except a single field from a nested document via projection in MongoDB

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:19:59

350 Views

Set the field you don’t want to include as 0. This would display rest of the values while using find(). Let us first create a collection with documents −> db.demo237.insertOne({ ...   _id:101, ...   Product: { ...      description1: {id:1001 }, ...      description2: {Name:"Product-1" }, ... ... Read More

How to sort, select and query subdocument in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:18:27

231 Views

To sort, use $sort in MongoDB. Let us create a collection with documents −> db.demo236.insertOne({"details":{"Name":"Chris", "Age":21}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e419015f4cebbeaebec514c") } > db.demo236.insertOne({"details":{"Name":"David", "Age":23}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e41901cf4cebbeaebec514d") } > db.demo236.insertOne({"details":{"Name":"Bob", "Age":24}}); {    "acknowledged" : true,    "insertedId" : ... Read More

Is there any way in MongoDB to get the inner value of json data?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:16:30

198 Views

To get inner value of JSON data, use find() along with dot(.) notation. Let us create a collection with documents −> db.demo235.insertOne( ...   { ...      "id":101, ...      "details":[ ...         { ...            "Name":"Chris Brown", ...     ... Read More

MongoDB query to fetch a document that does not have a particular field?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:14:08

543 Views

To check for existence, use $exists. Let us create a collection with documents − > db.demo234.insertOne({"FirstName":"Chris", "LastName":"Brown", "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e418a50f4cebbeaebec5148") } > db.demo234.insertOne({"FirstName":"David", "LastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e418a5ff4cebbeaebec5149") } > db.demo234.insertOne({"FirstName":"John", "LastName":"Smith", Age:34}); {    "acknowledged" : true,   ... Read More

How to limit the amount of characters returned from a field in a MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:12:40

432 Views

To limit the amount of characters returned from a field, use $substr in MongoDB. Let us create a collection with documents −> db.demo233.insertOne({"Paragraph":"My Name is John Smith.I am learning MongoDB database"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e41877df4cebbeaebec5146") } > db.demo233.insertOne({"Paragraph":"David Miller is a good student and learning ... Read More

Using MongoDB Aggregate and GroupBy to get the frequency of name record

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:09:36

173 Views

Let us first create a collection with documents −> db.demo232.insertOne({_id:101, Name:"Chris"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo232.insertOne({_id:102, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo232.insertOne({_id:103, Name:"Bob"}); { "acknowledged" : true, "insertedId" : 103 } > db.demo232.insertOne({_id:104, Name:"David"}); { "acknowledged" : true, "insertedId" : ... Read More

Best way to sum array size fields in MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:08:38

1K+ Views

To sum the array size fields, use $sum along with $size. Let us create a collection with documents −> db.demo231.insertOne({"Subjects":["MongoDB", "MySQL", "SQL Server"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc73ff4cebbeaebec5143") } > db.demo231.insertOne({"Subjects":["Java", "C", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc757f4cebbeaebec5144") } > db.demo231.insertOne({"Subjects":["Python", "Spring"]}); ... Read More

Usage of findOne() with MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 13:06:17

356 Views

The findOne() in MongoDB returns only a single document. Let us create a collection with documents −> db.demo230.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4d2f4cebbeaebec513e") } > db.demo230.insertOne({"FirstName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e3fc4d5f4cebbeaebec513f") } > db.demo230.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ... Read More

Advertisements