Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Database Articles
Page 240 of 546
MongoDB aggregate to convert multiple documents into single document with an array?
For aggregate in MongoDB, use aggregate(). Let us create a collection with documents −> db.demo248.insertOne({"id":101, "Name":"Chris", "Age":21, "CountryName":"US"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6651627c0c63e7dba6d") } > db.demo248.insertOne({"id":101, "Name":"Bob", "Age":22, "CountryName":"UK"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6741627c0c63e7dba6e") } > db.demo248.insertOne({"id":102, "Name":"Mike", "Age":20, "CountryName":"AUS"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b6811627c0c63e7dba6f") }Display all documents from a collection with the help of find() method −> db.demo248.find();This will produce the following output −{ "_id" : ObjectId("5e46b6651627c0c63e7dba6d"), "id" : 101, "Name" : "Chris", "Age" : 21, "CountryName" : "US" } { "_id" : ObjectId("5e46b6741627c0c63e7dba6e"), "id" : 101, ...
Read MoreMongoDB query to skip n first documents?
To skip a specific number of documents, use skip() along with limit. Let us create a collection with documents −> db.demo246.insertOne({"StudentFirstName":"Chris", "StudentLastName":"Brown"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0d71627c0c63e7dba65") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0e21627c0c63e7dba66") } > db.demo246.insertOne({"StudentFirstName":"John", "StudentLastName":"Smith"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0ea1627c0c63e7dba67") } > db.demo246.insertOne({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); { "acknowledged" : true, "insertedId" : ObjectId("5e46b0f91627c0c63e7dba68") }Display all documents from a collection with the help of find() method −> db.demo246.find();This will produce the following output −{ "_id" : ObjectId("5e46b0d71627c0c63e7dba65"), "StudentFirstName" : "Chris", "StudentLastName" : "Brown" } ...
Read MoreCoalesce values from different properties into a single array with MongoDB aggregation
To coalesce values means to merge them. To merge them into a single array, use $project in MongoDB.Let us create a collection with documents −> db.demo244.insertOne({"Value1":10, "Value2":20}); { "acknowledged" : true, "insertedId" : ObjectId("5e4582e31627c0c63e7dba63") } > db.demo244.insertOne({"Value1":20, "Value2":30}); { "acknowledged" : true, "insertedId" : ObjectId("5e4582f11627c0c63e7dba64") }Display all documents from a collection with the help of find() method −> db.demo244.find();This will produce the following output −{ "_id" : ObjectId("5e4582e31627c0c63e7dba63"), "Value1" : 10, "Value2" : 20 } { "_id" : ObjectId("5e4582f11627c0c63e7dba64"), "Value1" : 20, "Value2" : 30 }Following is the query to coalesce values from different properties into ...
Read MoreA single MongoDB query to orderby date and group by user
For this, simply use aggregate() in MongoDB. Let us create a collection with documents −> db.demo243.insertOne({"userId":1, dueDate:new ISODate("2019-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4575f81627c0c63e7dba5f") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2019-11-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576011627c0c63e7dba60") } > db.demo243.insertOne({"userId":2, dueDate:new ISODate("2020-01-31")}); { "acknowledged" : true, "insertedId" : ObjectId("5e4576151627c0c63e7dba61") } > db.demo243.insertOne({"userId":1, dueDate:new ISODate("2010-01-10")}); { "acknowledged" : true, "insertedId" : ObjectId("5e45761b1627c0c63e7dba62") }Display all documents from a collection with the help of find() method −> db.demo243.find();This will produce the following output −{ "_id" : ObjectId("5e4575f81627c0c63e7dba5f"), "userId" : 1, "dueDate" : ISODate("2019-01-10T00:00:00Z") } { ...
Read MoreEasiest way to sort an array in MongoDB
The easiest way to sort an array in MongoDB, use $sort. Let us create a collection with documents −> db.demo242.insertOne( ... ... {"details2": ... [ ... {"ShipingDate":new ISODate("2019-10-11"), "Price":1400}, ... {"ShipingDate":new ISODate("2019-10-01"), "Price":1600 } ... ] ... } ... ...); { "acknowledged" : true, "insertedId" : ObjectId("5e4429229af932883c61ea44") }Display all documents from a collection with the help of find() method −> db.demo242.find();This will produce the following output −{ "_id" : ObjectId("5e4429229af932883c61ea44"), "details2" : [ { "ShipingDate" : ISODate("2019-10-11T00:00:00Z"), "Price" : 1400 ...
Read MoreGet the maximum mark records from a collection with documents in MongoDB
To limit the number of records, use $limit in MongoDB. Let us create a collection with documents −> db.demo240.insertOne({"StudentName":"Chris"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d969af932883c61ea3c") } > db.demo240.insertOne({"StudentName":"Bob"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9a9af932883c61ea3d") } > db.demo240.insertOne({"StudentName":"David"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441d9d9af932883c61ea3e") } > db.demo240.insertOne({"StudentName":"Mike"}); { "acknowledged" : true, "insertedId" : ObjectId("5e441da19af932883c61ea3f") }Display all documents from a collection with the help of find() method −> db.demo240.find();This will produce the following output −{ "_id" : ObjectId("5e441d969af932883c61ea3c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e441d9a9af932883c61ea3d"), "StudentName" : "Bob" } { ...
Read MoreMongoDB query for documents whose array elements does not have a specific value
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" : [ ... { "DueDate" : new ISODate("2019-01-21"), "ProductPrice" : 1270 }, ... { "DueDate" : new ISODate("2020-02-12"), "ProductPrice" : 2000 } ... ] ... } ...); { "acknowledged" : true, "insertedId" : ObjectId("5e441c6bf4cebbeaebec5157") } > db.demo239.insertOne( ... { ...
Read MoreImplement MongoDB Aggregate - unwind, group and project?
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 the next stage in the pipeline.Let us create a collection with documents −> db.demo238.insertOne( ... { ... ... "EmailId" : "John@gmail.com", ... "details" : [ ... { ... "Name" : "Bob", ... ...
Read MoreRemove all except a single field from a nested document via projection in MongoDB
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" }, ... description3: {Price:550 } ... } ...} ...); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo237.find().pretty();This will produce the following output −{ "_id" : 101, "Product" : { "description1" : ...
Read MoreHow to sort, select and query subdocument in MongoDB?
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" : ObjectId("5e419023f4cebbeaebec514e") }Display all documents from a collection with the help of find() method −> db.demo236.find();This will produce the following output −{ "_id" : ObjectId("5e419015f4cebbeaebec514c"), "details" : { "Name" : "Chris", "Age" : 21 } } { "_id" : ObjectId("5e41901cf4cebbeaebec514d"), "details" : { "Name" : "David", "Age" : 23 } } ...
Read More