AmitDiwan has Published 10740 Articles

How to select specific columns in MongoDB query?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:28:16

7K+ Views

To select specific columns, you can ignore the rest of them i.e. to hide those columns, set them to 0. Let us first create a collection with documents −> db.demo415.insertOne({"ClientName":"Robert", "ClientCountryName":"US"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e72329db912067e57771adc") } > db.demo415.insertOne({"ClientName":"David", "ClientCountryName":"UK"}); {    "acknowledged" : true,   ... Read More

Find by _id on an array of objects in MongoDB database?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:26:08

871 Views

To find by _id on an array of objects, use aggregation and avoid using find(). Let us first create a collection with documents −> db.demo414.insertOne( ...    { ...       "_id": "110", ...       "details":[ ...          { ...         ... Read More

Is it possible to exclude nested fields in MongoDB with a wildcard?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:24:14

943 Views

Achieve this with aggregation pipeline. Let us first create a collection with documents −> db.demo413.insertOne( ...    { ...       "_id": "101", ...       "details": { ...          "Info1": { ...             Name:"Chris", ...         ... Read More

Update an array of strings nested within an array of objects in MongoDB

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:22:52

284 Views

Let us create a collection with documents −> db.demo411.aggregate( ...    [ ...       {$project : { ...          _id : 0, ...          Information : {$map : {input : "$Information", as : "out", in : ["$$out.Name1", "$$out.Name2"]}} ...       ... Read More

How to get only values in arrays with MongoDB aggregate?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:20:04

955 Views

Let us create a collection with documents −> db.demo411.insertOne( ...    { ...       "Information" : [ ...          { ...             "Name1" : "Chris", ...             "Name2" : "David" ...         ... Read More

How do I remove elements not matching conditions in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:18:01

429 Views

To remove elements, use $pull and for such conditions, use $ne. The $ne in MongoDB is used to select the documents where the value of the field is not equal to the specified value.Let us create a collection with documents −> db.demo410.insertOne( ...    { ...       details: ... Read More

How to find MongoDB documents with a specific string?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:15:44

236 Views

To find documents with a specific string, use find() and in that search for a string with regex. Let us create a collection with documents −> db.demo409.insertOne({"Name":"John Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4e515dc524f7022767c") } > db.demo409.insertOne({"Name":"Chris Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e4ec15dc524f7022767d") ... Read More

Understanding MongoDB Query Plan

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:13:53

585 Views

To get a query plan in MongoDB, use explain(). The $explain operator gives information on the query plan.Let us create a collection with documents −> db.demo408.insertOne({"Value":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3a115dc524f70227678") } > db.demo408.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e70e3a715dc524f70227679") } > db.demo408.insertOne({"Value":45}); ... Read More

How to use arrays as filters by querying subdocuments in MongoDB?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:08:58

154 Views

For this, use $setIsSubset in MongoDB. Let us create a collection with documents −> db.demo407.insertOne( ...    { ...       Name:"Chris", ...       "details" : [ ...          { ...             id:100 ...          }, ... Read More

How to create MongoDB user on existing database with correct role?

AmitDiwan

AmitDiwan

Updated on 03-Apr-2020 13:01:56

296 Views

To create a new user in MongoDB, use createUser(). Following is the query −> db.createUser( ...    { ...       user: "John", ...       pwd: "123456", ...       roles: [ { role: "readWrite", db: "test" } ], ...       mechanisms: [ "SCRAM-SHA-256" ... Read More

Advertisements