AmitDiwan has Published 10744 Articles

How to query documents by a condition on the subdocument in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 14:24:10

203 Views

Let us first create a collection with documents −> db.demo394.insertOne( ...    { ... ...       details: [ ...       { ...          _id: '1', ...          startDate: '2018-01-11T07:00:00.000Z', ...          endDate: '2019-01-12T07:59:59.999Z' ...       ... Read More

MongoDB query to collectively match intersection of documents against a field

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 14:21:18

380 Views

For this, use aggregate(). Let us first create a collection with documents −> db.demo393.insertOne( ...    { ...       Id1: "1", ...       Name: "Chris", ...       Id2: "100" ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5e6dd522064be7ab44e804") } ... Read More

MongoDB query to pull multiple values from array

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 14:16:43

541 Views

To pull values, use $pull and set multi: true. Let us first create a collection with documents −> db.demo392.insertOne( ...    { ...       Name: 'Chris', ...       details: [ ...          { ...             _id: '101' ... ... Read More

Get a single element from the array of results by index in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 14:12:59

546 Views

To get a single element, use aggregation and LIMIT. The skip() is used to skip a specific number of documents.Let us first create a collection with documents −> db.demo391.insertOne( ...    { "_id" : 101, "Name" : "Chris", Values: ["101", "102"] } ... ) { "acknowledged" : true, "insertedId" : ... Read More

Update values in multiple documents with multi parameter in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 14:09:16

438 Views

You need to set multi to true. Include the option multi − true to update all documents that match the query criteria.Let us first create a collection with documents −> db.demo390.insertOne({"FirstName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e5d1f3a22064be7ab44e7fa") } > db.demo390.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" ... Read More

Extract all the values and display in a single line with MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:59:03

667 Views

Let us first create a collection with documents −> db.demo389.insertOne( ...    { ...       "details":[ ...          { ...             "Name":[ ...                "Chris", ...                "David" ... Read More

MongoDB query to remove element from array as sub property

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:55:18

392 Views

To remove, use $pull in MongoDB. Let us first create a collection with documents −> db.demo388.insertOne( ...    { ...       _id: '101', ...       userDetails: { ...          isMarried: false, ...          userInfo: [ ...         ... Read More

MongoDB query to unwind two arrays

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:52:58

686 Views

To unwind, means to deconstruct an array field from the input documents to output a document for each element.To unwind arrays, use $unwind in MongoDB aggregation. Let us first create a collection with documents −> db.demo387.insertOne( ...    { ... ...       "Name" : "101", ...     ... Read More

Convert date parts to date in MongoDB

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:49:36

323 Views

Let us first create a collection with documents −> db.demo386.insert( ...    { ...       details: { Month: 02, Day: 27, Year: 2020 } ...    } ... ); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo386.find();This will ... Read More

Update multiple elements in an array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 02-Apr-2020 13:47:05

2K+ Views

To update multiple elements, use $[]. The $[] is an all positional operator indicating that the update operator should modify all elements in the specified array field.Let us first create a collection with documents −> db.demo385.insertOne({"ServerLogs": [ ...       { ...          "status":"InActive" ...   ... Read More

Advertisements