Query Only the Field Name and Display ID in MongoDB

AmitDiwan
Updated on 12-May-2020 08:57:02

895 Views

To query only the field name, set fieldName to 0 i.e. the fieldName to hide. Let us create a collection with documents −> db.demo650.insertOne({_id:101, details:{Name:"Chris", Age:21}}); { "acknowledged" : true, "insertedId" : 101 } > db.demo650.insertOne({_id:102, details:{Name:"Bob", Age:22}}); { "acknowledged" : true, "insertedId" : 102 } > db.demo650.insertOne({_id:103, details:{Name:"Sam", Age:20}}); { "acknowledged" : true, "insertedId" : 103 } > db.demo650.insertOne({_id:104, details:{Name:"Robert", Age:24}}); { "acknowledged" : true, "insertedId" : 104 }Display all documents from a collection with the help of find() method −> db.demo650.find();This will produce the following output −{ "_id" : 101, "details" : { "Name" : "Chris", "Age" : ... Read More

MongoDB Aggregation: Group and Remove Duplicate Array Values

AmitDiwan
Updated on 12-May-2020 08:55:19

3K+ Views

Use MongoDB aggregate for this and within that, use $group. Let us create a collection with documents −> db.demo649.insertOne( ...    { "_id" : 101, "Names" : [ "John", "Bob", "Bob", "Robert" ], "CountryName" : "US" } ... ); { "acknowledged" : true, "insertedId" : 101 } > > db.demo649.insertOne({ "_id" :102, "Names" : [ "John", "Robert" ], "CountryName" : "UK"}); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo649.find();This will produce the following output −{ "_id" : 101, "Names" : [ "John", "Bob", "Bob", "Robert" ], "CountryName" ... Read More

Create and Display Newly Created Database in MongoDB

AmitDiwan
Updated on 12-May-2020 08:53:29

138 Views

To create a new database, you need to use the USE command as in the below syntax −use yourDatabaseName;To show all databases, you need to use show command. The syntax is as follows −show dbs;Let us implement the above syntax in order to create database −> use onlinecustomertracker; switched to db onlinecustomertrackerIn order to display the newly created database, you need to create collection. Following is the query −> db.example.insert({Value:10}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.example.find();This will produce the following output −{ "_id" : ObjectId("5e9dafade3c3cd0dcff36a4f"), "Value" : 10 ... Read More

Perform Nested Document Value Search in MongoDB

AmitDiwan
Updated on 12-May-2020 08:50:43

160 Views

For searching value, use $match in MongoDB. Let us create a collection with documents −> db.demo648.insertOne( ...    { ...       StudentInformation: ...       [ ...          { ...             Name:"John", ...             CountryName:"US" ...          }, ...          { ...             Name:"David", ...             CountryName:"AUS" ...          }, ...          { ...             Name:"Chris", ... ... Read More

MongoDB Aggregation to Combine or Merge Fields and Then Count

AmitDiwan
Updated on 12-May-2020 08:47:14

662 Views

To combine or merge fields and then perform count, use $group along with $sum and $sort. Let us create a collection with documents −> db.demo647.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86316c954c74be91e6ee") } > db.demo647.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86356c954c74be91e6ef") } > db.demo647.insertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86376c954c74be91e6f0") } > db.demo647.insertOne({"Subject":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86406c954c74be91e6f1") } > db.demo647.insertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c86436c954c74be91e6f2") } > db.demo647.insertOne({"Subject":"PL/SQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c864b6c954c74be91e6f3") } > db.demo647.insertOne({"Subject":"MongoDB"}); {   ... Read More

Move Different Elements to Another Array in MongoDB

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

349 Views

Use forEach and check for the different elements and use save() along with some condition. Let us create a collection with documents −> db.demo646.insertOne( ...    { ... ...       "Information": [ ...          { id: 100, Name:"Chris" }, ...          { id: 100, Name:"Chris" }, ...          { id: 101, Name:"David" }, ...          { id: 100, Name:"Chris" } ...       ], ...       "different": [] ...    } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c82ec6c954c74be91e6ed") }Display ... Read More

MongoDB Query to Store File Name and Location

AmitDiwan
Updated on 12-May-2020 08:37:32

582 Views

To store, let us see an example and create a collection with documents −> db.demo645.insertOne( ...    { ...       'fileName' : 'MongoDB Program', ...       'fileLocation':'C:/users/workspace/AllMongoDBProgram/MongoDB Program' ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c78f36c954c74be91e6e8") } > > db.demo645.insertOne( ...    { ...       'fileName' : 'SumOfTwoNumbers.java', ...       'fileLocation':'E:/eclipseWorkspace/AllJavaPrograms/SumOfTwoNumbers.java' ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c78f36c954c74be91e6e9") } > db.demo645.insertOne( ...    { ...       'fileName' : 'Script.sql', ...       'fileLocation':'C:/MySQLQuery/Script.sql' ...    } ... Read More

Update Timestamp and Set to Current Date in MongoDB

AmitDiwan
Updated on 12-May-2020 08:35:35

3K+ Views

To update, use update() in MongoDB. To set it to current date, you need to get the current date −var todayDate = new Date();Let us first create a collection with documents −> db.demo644.insertOne({"ShippingDate":new ISODate("2018-04-19")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c76896c954c74be91e6e6") } > db.demo644.insertOne({"ShippingDate":new ISODate("2019-01-10")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c76966c954c74be91e6e7") }Display all documents from a collection with the help of find() method −> db.demo644.find();This will produce the following output −{ "_id" : ObjectId("5e9c76896c954c74be91e6e6"), "ShippingDate" : ISODate("2018-04-19T00:00:00Z") } { "_id" : ObjectId("5e9c76966c954c74be91e6e7"), "ShippingDate" : ISODate("2019-01-10T00:00:00Z") }Following is the query to update timestamp −> var todayDate ... Read More

Updating Nested Embedded Documents in MongoDB

AmitDiwan
Updated on 12-May-2020 08:31:10

1K+ Views

To update bested documents in MongDB, use UPDATE() and positional($) operator. Let us create a collection with documents −> db.demo643.insertOne({ ...    details : [ ...       { ...          "CountryName":"US", ...          StudentDetails:[{Name:"Chris"}, {SubjectName:"MySQL"}] ...       }, ... ...       { ...          "CountryName":"UK", ...          StudentDetails:[{Name:"Bob"}, {SubjectName:"Java"}] ...       } ...    ] ... } ... ) {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c737f6c954c74be91e6e3") }Display all documents from a collection with the help of find() method ... Read More

MongoDB Query to Get Specific List of Names from Documents with Array Field

AmitDiwan
Updated on 12-May-2020 08:24:42

298 Views

For this, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo642.insertOne( ...    { ...       _id:1, ...       ListOfNames:["Robert", "John"] ...    } ... ); { "acknowledged" : true, "insertedId" : 1 } > db.demo642.insertOne( { _id:2, ListOfNames:["Robert", "Chris"] } ); { "acknowledged" : true, "insertedId" : 2 }Display all documents from a collection with the help of find() method −> db.demo642.find();This will produce the following output −{ "_id" : 1, "ListOfNames" ... Read More

Advertisements