AmitDiwan has Published 10744 Articles

Move different elements to another array in MongoDB?

AmitDiwan

AmitDiwan

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

328 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" }, ...          { ... Read More

MongoDB query to store File Name and location?

AmitDiwan

AmitDiwan

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

564 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( ... Read More

How to update a Timestamp and set to current date in MongoDB?

AmitDiwan

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" ... Read More

Updating Nested Embedded Documents in MongoDB?

AmitDiwan

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"}] ...       }, ... ... ... Read More

MongoDB query to get specific list of names from documents where the value of a field is an array

AmitDiwan

AmitDiwan

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

277 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"] ...   ... Read More

Is there a way to list collections in MongoDB?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:19:33

184 Views

To list collections, use getCollectionNames() in MongoDB. Following is the syntax −db.getCollectionNames();Let us implement the above syntax in order to list all collection names from the test database −> db.getCollectionNames();This will produce the following output −[    "arrayDemo",    "arrayFieldIsNotEmptyDemo",    "characterInFieldsDemo",    "checkFieldExistDemo",    "compareTwoFields",    "comparingTwoFieldsDemo",    "convertTextToDateTypeDemo", ... Read More

Get specific elements from embedded array in MongoDB?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:15:34

395 Views

To get specific elements, use $match with dot notation. Let us create a collection with documents −> db.demo641.insert( ...    { ...       ProductId:101, ...       "ProductInformation": ...      (                            [ ... ... Read More

Projection of arrays to get the first array element from MongoDB documents

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:11:50

693 Views

If you want the first element from array, you can use $slice along with $gte. Let us create a collection with documents −> db.demo640.insertOne({Name:"John", "Scores":[80, 90, 75]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e9c2eb86c954c74be91e6e0") } > db.demo640.insertOne({Name:"Chris", "Scores":[85, 70, 89]}); {    "acknowledged" : true,    "insertedId" : ... Read More

MongoDB aggregation framework with group query example?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:10:13

163 Views

For this, use $group in MongoDB aggregation. Let us create a collection with documents −> db.demo639.insertOne( ...    { ...       "_id" : 1, ...       "CountryName" : "US", ...       "Info1" : { ...          "Name" : "Chris", ...   ... Read More

Filtering MongoDB items by fields and subfields?

AmitDiwan

AmitDiwan

Updated on 12-May-2020 08:05:22

499 Views

To filter items by fields and subfields, use dot notation. Let us create a collection with documents −> db.demo638.insert({Name:"Chris"}); WriteResult({ "nInserted" : 1 }) > db.demo638.insert({Name:"David", details:{Subject:"MongoDB"}}); WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method −> db.demo638.find().pretty();This will produce the following output ... Read More

Advertisements