Split MongoDB Query and Skip Values

AmitDiwan
Updated on 12-May-2020 07:38:42

280 Views

To skip values in MongoDB, use skip() along with limit(). For 5 values, use limit(5). Let us create a collection with documents −> db.demo633.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0be76c954c74be91e6c1") } > db.demo633.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bea6c954c74be91e6c2") } > db.demo633.insertOne({"Value":30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bec6c954c74be91e6c3") } > db.demo633.insertOne({"Value":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bef6c954c74be91e6c4") } > db.demo633.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bf16c954c74be91e6c5") } > db.demo633.insertOne({"Value":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c0bf36c954c74be91e6c6") } > db.demo633.insertOne({"Value":70}); {    "acknowledged" : ... Read More

Remove Values from a Matrix-like Document in MongoDB

AmitDiwan
Updated on 12-May-2020 07:33:27

162 Views

To remove values from a matrix, use $pull in MongoDB. Let us create a collection with documents −> db.demo632.insertOne( ...    { ...       "arrayMatrix": [ ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20], ...          [10, 20] ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b27b46c954c74be91e6c0") }Display all documents from a collection with the help of find() ... Read More

Return Specific MongoDB Embedded Document

AmitDiwan
Updated on 12-May-2020 07:23:41

234 Views

Use $unwind twice for specific embedded document in MongoDB. Let us create a collection with documents −> db.demo631.insert( ...    { ...       id: "101", ...       Info1: [ ...          { ...             CountryName : "US", ...             Info2 : [ ...                { ...                   Name:"Chris", ...                   Age:24 ...                }, { ... Read More

Sort MongoDB Documents in Descending Order

AmitDiwan
Updated on 12-May-2020 07:19:13

341 Views

To sort documents, use sort() along with find(). Let us create a collection with documents −> db.demo630.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b080e6c954c74be91e6ba") } > db.demo630.insertOne({"Value":200}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b08116c954c74be91e6bb") } > db.demo630.insertOne({"Value":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b08146c954c74be91e6bc") } > db.demo630.insertOne({"Value":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9b08176c954c74be91e6bd") }Display all documents from a collection with the help of find() method −> db.demo630.find();This will produce the following output −{ "_id" : ObjectId("5e9b080e6c954c74be91e6ba"), "Value" : 10 } { "_id" : ObjectId("5e9b08116c954c74be91e6bb"), "Value" : 200 } { "_id" : ... Read More

Filter Documents in MongoDB Using Simple Query

AmitDiwan
Updated on 12-May-2020 07:17:36

223 Views

You can use $match. The $match filters the documents to pass only the documents that match the specified condition to the next pipeline stage. Let us create a collection with documents −> db.demo629.insertOne( ...    { ... ...       "Subject": [ ...          "MySQL", ...          "MongoDB" ...       ], ...       "details": [ ...          { ...             Name:"Chris", ...             "Marks":78 ...          }, ...          { ... Read More

Query for Values Not Objects in List with MongoDB

AmitDiwan
Updated on 12-May-2020 07:13:29

157 Views

To query for values in list, use positional operator($) in MongoDB. Let us create a collection with documents −> db.demo628.insertOne({id:1, Name:["Chris", "David", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7ea6c954c74be91e6b6") } > db.demo628.insertOne({id:1, Name:["Carol", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae7f26c954c74be91e6b7") } > db.demo628.insertOne({id:2, Name:["Mike", "Sam", "John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ae8056c954c74be91e6b8") }Display all documents from a collection with the help of find() method −> db.demo628.find();This will produce the following output −{ "_id" : ObjectId("5e9ae7ea6c954c74be91e6b6"), "id" : 1, "Name" : [ "Chris", "David", "John" ] } { "_id" : ObjectId("5e9ae7f26c954c74be91e6b7"), "id" ... Read More

Sum with MongoDB Group By Multiple Columns to Calculate Total Marks

AmitDiwan
Updated on 12-May-2020 07:11:54

816 Views

For this, use aggregate() along with $group. Let us create a collection with documents −> db.demo627.insertOne({id:101, "Name":"Chris", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb306c954c74be91e6b2") } > db.demo627.insertOne({id:102, "Name":"Bob", "Marks":74}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb3c6c954c74be91e6b3") } > db.demo627.insertOne({id:101, "Name":"Chris", "Marks":87}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb426c954c74be91e6b4") } > db.demo627.insertOne({id:102, "Name":"Mike", "Marks":70}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9acb4b6c954c74be91e6b5") }Display all documents from a collection with the help of find() method −> db.demo627.find();This will produce the following output −{ "_id" : ObjectId("5e9acb306c954c74be91e6b2"), "id" : 101, "Name" : "Chris", "Marks" : ... Read More

Get Data of Array Intersection in MongoDB

AmitDiwan
Updated on 12-May-2020 07:07:10

541 Views

For array interection in MongoDB, use the $setIntersection in aggregate(). Let us create a collection with documents −> db.demo625.insertOne( ...    { ...       Name: "John", ...       Marks: [56, 98, 60] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab8e16c954c74be91e6aa") } > db.demo625.insertOne( ...    { ...       Name: "John", ...       Marks: [110, 56, 72] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9ab8e26c954c74be91e6ab") } > db.demo625.insertOne( ...    { ...       Name: "Chris", ...       ... Read More

Create Normal and Compound Indexes in MongoDB

AmitDiwan
Updated on 12-May-2020 06:59:31

109 Views

Yes, you can use ensureIndex(). MongoDB provides complete support for indexes on any field in a collection of documents.Let us create a collection with documents −> db.demo622.ensureIndex({_id:1, Name:1, Age:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo622.insertOne({_id:101, Name:"Chris", Age:21}); { "acknowledged" : true, "insertedId" : 101 } > db.demo622.insertOne({_id:102, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 102 } > db.demo622.insertOne({_id:103, Name:"Bob", Age:21}); { "acknowledged" : true, "insertedId" : 103 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); { "acknowledged" : true, "insertedId" : 104 } > db.demo622.insertOne({_id:104, Name:"Chris", Age:22}); 2020-04-18T12:21:18.085+0530 ... Read More

Aggregate Second Element from Input Element in MongoDB

AmitDiwan
Updated on 12-May-2020 06:57:19

125 Views

To aggregate second element from input element, use mapReduce(). Map-reduce is a data processing paradigm for condensing large volumes of data into useful aggregated results. Let us create a collection with documents −> db.demo621.insert({ _id: 101, Name1: "John", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 102, Name1: "Bob", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 103, Name1: "Chris", Name2: "John" }); WriteResult({ "nInserted" : 1 }) > db.demo621.insert({ _id: 104, Name1: "Sam", Name2: "John" }); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> ... Read More

Advertisements