Found 6705 Articles for Database

How to aggregate two lists if at least one element matches in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:17:18

283 Views

For this, use $group in MongoDB. Within that, use $unwind, $group, $addToSet, etc. Let us create a collection with documents −> db.demo456.insertOne( ... { _id: 101, StudentName: ["Chris", "David"] } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo456.insertOne( ... { _id: 102, StudentName: ["Mike", "Sam"] } ... ); { "acknowledged" : true, "insertedId" : 102 } > db.demo456.insertOne( ... { _id: 103, StudentName: ["John", "Jace"] } ... ); { "acknowledged" : true, "insertedId" : 103 } > db.demo456.insertOne( ... { _id: 104, StudentName: ["Robert", "John"] } ... ); { "acknowledged" : true, "insertedId" : 104 ... Read More

How do I display a list of objects based on a specific property with MongoDB?

AmitDiwan
Updated on 11-May-2020 09:16:38

336 Views

To display a list of objects based on a specific property, use dot notation in find(). Let us create a collection with documents −> db.demo455.insertOne({"Information":{"Student":[{"Name":"Chris", "Age":22}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1876dbcb9adb296c95c5") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"David", "Age":21}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e1883dbcb9adb296c95c6") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"Bob", "Age":24}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e188adbcb9adb296c95c7") } > db.demo455.insertOne({"Information":{"Student":[{"Name":"Robert", "Age":21}]}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7e18bcdbcb9adb296c95c8") }Display all documents from a collection with the help of find() method −> db.demo455.find();This will produce the following output −{ "_id" : ObjectId("5e7e1876dbcb9adb296c95c5"), "Information" : { "Student" : [ { ... Read More

How to continuously publish the latest N records with sorting using MongoDB?

AmitDiwan
Updated on 11-May-2020 09:14:18

85 Views

To publish the latest N records with sorting, use sort() along with limit(). Here, set the number of records you want to show with limit(). Let us create a collection with documents −> db.demo454.insertOne({"ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce8cdbcb9adb296c95c0") } > db.demo454.insertOne({"ClientName":"John"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce95dbcb9adb296c95c1") } > db.demo454.insertOne({"ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cce9fdbcb9adb296c95c2") } > db.demo454.insertOne({"ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7ccea6dbcb9adb296c95c3") } > db.demo454.insertOne({"ClientName":"Mike"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7cceafdbcb9adb296c95c4") }Display all documents from a collection with the help of find() method −> db.demo454.find();This ... Read More

How to filter documents based on an array in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:13:33

1K+ Views

To filter documents based on an array, use $elemMatch. The $elemMatch operator matches documents that contain an array field.Let us create a collection with documents −> db.demo453.insertOne( ... { _id: 101, details: [ { Name: "David", Marks: 60 }, { Name: "Mike", Marks: 55} ] } ... ) { "acknowledged" : true, "insertedId" : 101 } > db.demo453.insertOne( ... { _id: 102, details: [ { Name: "Bob", Marks: 80 }, { Name: "Sam", Marks: 78} ] } ... ) { "acknowledged" : true, "insertedId" : 102 } > db.demo453.insertOne( ... { _id: 103, details: [ { Name: "Carol", Marks: 67 ... Read More

Get the aggregated result and find the count of repeated values in different MongoDBdocuments

AmitDiwan
Updated on 11-May-2020 09:11:28

169 Views

To get the count of repeated values in different documents, use aggregate(). Let us create a collection with documents −> db.demo452.insertOne({"StudentName":"John", "StudentAge":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3371f552a0ebb0a6f3") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3671f552a0ebb0a6f4") } > db.demo452.insertOne({"StudentName":"John", "StudentAge":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e3971f552a0ebb0a6f5") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4371f552a0ebb0a6f6") } > db.demo452.insertOne({"StudentName":"David", "StudentAge":25});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b7e4571f552a0ebb0a6f7") }Display all documents from a collection with the help of find() method −> db.demo452.find();This will produce the following output −{ "_id" : ... Read More

MongoDB aggregate to get the Mean daily average count of recorded documents in a collection?

AmitDiwan
Updated on 11-May-2020 10:07:14

850 Views

To get the mean daily average count of recorded documents, use aggregate(). Within that, use $project and $group.Let us create a collection with documents −Example> db.demo451.insertOne({ ... DueDate:new ISODate("2020-03-15T10:50:35.000Z"), ... Value: 10 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b5c5d71f552a0ebb0a6e9") } > db.demo451.insertOne({ ... DueDate:new ISODate("2020-03-14T10:50:35.000Z"), ... Value: 10 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b5c5d71f552a0ebb0a6ea") } > db.demo451.insertOne({ ... DueDate:new ISODate("2020-03-13T10:50:35.000Z"), ... Value: 10 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b5c5d71f552a0ebb0a6eb") }Display all documents from a collection with the help of find() ... Read More

How to reach subdata in MongoDB and display a particular document?

AmitDiwan
Updated on 11-May-2020 09:02:59

163 Views

In order to reach subdata, you need to use key in MongoDB. Let us create a collection with documents −>db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris", "StudentAge":21}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7b590e71f552a0ebb0a6e6") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"David", "StudentAge":23}}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b591a71f552a0ebb0a6e7") } >db.demo450.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike", "StudentAge":22}}});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7b592271f552a0ebb0a6e8") }Display all documents from a collection with the help of find() method −> db.demo450.find();This will produce the following output −{ "_id" : ObjectId("5e7b590e71f552a0ebb0a6e6"), "Information" : { "StudentDetails" : { "StudentName" : "Chris", "StudentAge" : 21 } } } { "_id" : ObjectId("5e7b591a71f552a0ebb0a6e7"), "Information" : { "StudentDetails" : { ... Read More

How to insert an item to an array that is inside an object in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:02:05

883 Views

To insert an item to an already created array inside an object, use MongoDB $push. Let us create a collection with documents −> db.demo449.insertOne( ... { ...    details1: { ...       details2: [{ ...          _id:new ObjectId(), ...             Name:"Chris" ...       }], ...       details3: [{ ...          _id:new ObjectId(), ...          Name:"David" ...       }] ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7a40e971f552a0ebb0a6e3") }Display all documents from ... Read More

What is the difference between deleteOne() and findOneAndDelete() operation in MongoDB?

AmitDiwan
Updated on 11-May-2020 08:59:12

3K+ Views

The findOneAndDelete() deletes single documents from the collection on the basis of a filter and sort criteria as well as it returns the deleted document.The deleteOne() removes single document from the collection.Let us see an example and create a collection with documents −> db.demo448.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a291cbbc41e36cc3caeca") } > db.demo448.insertOne({"Name":"David", "Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a2926bbc41e36cc3caecb") } > db.demo448.insertOne({"Name":"Bob", "Age":22});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7a2930bbc41e36cc3caecc") }Display all documents from a collection with the help of find() method −> db.demo448.find();This will produce the following output −{ "_id" : ObjectId("5e7a291cbbc41e36cc3caeca"), ... Read More

Difference between Inverted Index and Forward Index

Mahesh Parahar
Updated on 15-Apr-2020 08:22:52

2K+ Views

Inverted Index and Forward Index are data structures used to search text in a document or set of documents.Inverted IndexInverted Index stores the words as index and document name(s) as mapped reference(s).Forward IndexForward Index stores the document name as index and word(s) as mapped reference(s).The following are some of the important differences between the Inverted Index and Forward Index.Sr. No.KeyInverted IndexForward Index1Mapping PatternInverted Index stores the words as index and document name(s) as mapped reference(s).Forward Index stores the document name as index and word(s) as mapped reference(s).2Index Building ProcessScan the document, prepare a list of unique words.Prepare a list of ... Read More

Advertisements