AmitDiwan has Published 8358 Articles

How to get the index of an array element in older versions on MongoDB?

AmitDiwan

AmitDiwan

Updated on 30-Mar-2020 07:23:50

204 Views

To get index of an array element, use $indexOfArray. Let us create a collection with documents −> db.demo65.insertOne({"ListOfValues":[10, 20, 30]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28990ecfb11e5c34d89938") } > db.demo65.insertOne({"ListOfValues":[50, 60, 70, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e28991ecfb11e5c34d89939") } > db.demo65.insertOne({"ListOfValues":[30, 40, 89, 91, ... Read More

MongoDB aggregation to sum product price with similar IDs

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:51:23

462 Views

You need to use $group to group documents with specified _id expression. Let us first create a collection with documents −> db.aggreagateDemo.insertOne({"Product_Id":1, "ProductPrice":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3c025ddae1f53b621d9") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":100}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d3c625ddae1f53b621da") } > db.aggreagateDemo.insertOne({"Product_Id":2, "ProductPrice":500}); { ... Read More

How to properly use 'exist' function in MongoDB like in SQL?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:34:34

189 Views

To check for existence of a record, use findOne() in MongoDB. Let us first create a collection with documents −> db.existsAlternateDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d23f9e4dae213890ac5c") } > db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06d2559e4dae213890ac5d") } >db.existsAlternateDemo.insertOne({"StudentName":"Chris", "StudentAge":22, "StudentCountryName":"US"}); {    "acknowledged" ... Read More

Insert in MongoDB without duplicates

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:29:48

8K+ Views

To insert records in MongoDB and avoid duplicates, use “unique:true”. Let us first create a collection with documents.Here, we are trying to add duplicate records −> db.insertWithoutDuplicateDemo.createIndex({"StudentFirstName":1}, { unique: true } ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } ... Read More

Apply a condition inside subset in MongoDB Aggregation?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:26:20

373 Views

To apply a condition, use $setIsSubset. Let us first create a collection with documents −> db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e063e49150ee0e76c06a052") } > db.subsetDemo.insertOne({"StudentName":"Chris", "StudentFavouriteSubject":["Java", "Python", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e063e4f150ee0e76c06a053") }Following is the query to display all documents ... Read More

Getting MongoDB results from the previous month

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:23:00

1K+ Views

At first, get the current month and subtract by 1 to fetch previous month records. Let us first create a collection with documents −> db.findOneMonthAgoData.insertOne({"CustomerName":"Chris", "PurchaseDate":new ISODate("2019-12-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e16c150ee0e76c06a04f") } > db.findOneMonthAgoData.insertOne({"CustomerName":"David", "PurchaseDate":new ISODate("2019-11-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e178150ee0e76c06a050") ... Read More

How can I aggregate nested documents in MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:20:33

1K+ Views

To aggregate nested documents in MongoDB, you can use $group. Let us first create a collection with documents −> db.aggregateDemo.insertOne( ...    { ...       "ProductInformation": [ ...          { ...             "Product1": [ ...           ... Read More

How to add a column in MongoDB collection?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 12:05:42

9K+ Views

To add a column, you need to update the collection. The syntax is as follows −db.getCollection(yourCollectionName).update({}, {$set: {"yourColumnName": "yourValue"}}, false, true);To understand the above syntax, let us create a collection with documents −> db.addColumnDemo.insertOne({"StudentId":101, "StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d66af5e889d7a519950f") } > db.addColumnDemo.insertOne({"StudentId":102, "StudentName":"Robert"}); {   ... Read More

How to get specific data in different formats with MongoDB?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:59:38

157 Views

For this, simply use find(). For a different format, use pretty(). Let us first create a collection with documents −> db.getSpecificData.insertOne( ... { ...    "StudentName": "John", ...    "Information": { ...       "FatherName": "Chris", ...       "Place": { ...          "CountryName": "US", ... Read More

MongoDB - How to copy rows into a newly created collection?

AmitDiwan

AmitDiwan

Updated on 27-Mar-2020 11:54:39

382 Views

To copy rows into another collection, use MongoDB. The syntax is as follows wherein “yourOldCollectionName” is the old collection, whereas where this collection will get copied is our new collection i.e. “yourNewCollectionName” −db.yourOldCollectionName.aggregate([{ $sample: { size: 333333 }}, {$out: "yourNewCollectionName"} ], {allowDiskUse: true});Let us first create a collection with documents ... Read More

Advertisements