Found 1659 Articles for Big Data Analytics

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

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

105 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" : true,    "insertedId" : ObjectId("5e06d2689e4dae213890ac5e") }Following is the query to display all documents from a collection with the help of find() method −> db.existsAlternateDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06d23f9e4dae213890ac5c"), "StudentName" : "Chris" } { "_id" : ObjectId("5e06d2559e4dae213890ac5d"), "StudentName" : "Chris", "StudentAge" : 21 } { ... Read More

MongoDB query to sum specific fields

AmitDiwan
Updated on 27-Mar-2020 12:32:18

2K+ Views

To sum specific fields, use aggregate along with $sum. Let us first create a collection with documents −> db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":50, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cec29e4dae213890ac55") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":102, "Price":200, "Status":"Inactive"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ced19e4dae213890ac56") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":101, "Price":3000, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cedd9e4dae213890ac57") } > db.getSumOfFieldsDemo.insertOne({"Customer_Id":103, "Price":400, "Status":"Active"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06cee79e4dae213890ac58") }Following is the query to display all documents from a collection with the help of find() method −> db.getSumOfFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ... Read More

Insert in MongoDB without duplicates

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

7K+ 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 } > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"Chris"}, { upsert: true }); WriteResult({ "nInserted" : 1 }) > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"David"}, { upsert: true }); WriteResult({ "nInserted" : 1 }) > db.insertWithoutDuplicateDemo.insert({"StudentFirstName":"Chris"}, { upsert: true }); WriteResult({    "nInserted" : 0,    "writeError" : {       "code" : 11000,       "errmsg" : "E11000 duplicate ... Read More

Apply a condition inside subset in MongoDB Aggregation?

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

288 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 from a collection with the help of find() method −> db.subsetDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e063e49150ee0e76c06a052"),    "StudentName" : "Chris",    "StudentFavouriteSubject" : [       "Java",       "Python"    ] } {    "_id" : ObjectId("5e063e4f150ee0e76c06a053"),    "StudentName" : "Chris",   ... Read More

Getting MongoDB results from the previous month

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

951 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") } > db.findOneMonthAgoData.insertOne({"CustomerName":"Bob", "PurchaseDate":new ISODate("2020-11-26")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04e186150ee0e76c06a051") }Following is the query to display all documents from a collection with the help of find() method −> db.findOneMonthAgoData.find();This will produce the following output −{ "_id" : ObjectId("5e04e16c150ee0e76c06a04f"), "CustomerName" : "Chris", "PurchaseDate" : ISODate("2019-12-26T00:00:00Z") } { ... Read More

How can I aggregate nested documents in MongoDB?

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": [ ...                { ...                   Amount: 50 ...                }, ...                { ...                   Amount: 90 ...         ... Read More

Update _id field in MongoDB

AmitDiwan
Updated on 27-Mar-2020 12:09:13

392 Views

To update, just save new ID and remove the old one using remove(). Let us first create a collection with documents −> db.updatingDemo.insertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04dae5150ee0e76c06a04b") } > db.updatingDemo.insertOne({"StudentName":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04dae7150ee0e76c06a04c") }Following is the query to display all documents from a collection with the help of find() method −> db.updatingDemo.find();This will produce the following output −{ "_id" : ObjectId("5e04dae5150ee0e76c06a04b"), "StudentName" : "Robert" } { "_id" : ObjectId("5e04dae7150ee0e76c06a04c"), "StudentName" : "Bob" }Here is the query to update _id in MongoDB −> myDocument = db.updatingDemo.findOne({"StudentName":"Bob"}); { "_id" : ObjectId("5e04dae7150ee0e76c06a04c"), ... Read More

How to add a column in MongoDB collection?

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

8K+ 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"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d673f5e889d7a5199510") } > db.addColumnDemo.insertOne({"StudentId":103, "StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d67bf5e889d7a5199511") }Following is the query to display all documents from a collection with the help of find() method −> db.addColumnDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e04d66af5e889d7a519950f"),   ... Read More

MongoDB query to test if a value is in array?

AmitDiwan
Updated on 27-Mar-2020 12:02:38

359 Views

To check for a specific value, use $in. Let us first create a collection with documents −> db.testInArray.insertOne({"ListOfNumbers":[10, 56, 78, 90, 32]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d42df5e889d7a519950d") } > db.testInArray.insertOne({"ListOfNumbers":[56, 78, 91, 100]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e04d588f5e889d7a519950e") }Following is the query to display all documents from a collection with the help of find() method −> db.testInArray.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e04d42df5e889d7a519950d"),    "ListOfNumbers" : [       10,       56,       78,       90,       32   ... Read More

How to get specific data in different formats with MongoDB?

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

64 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", ...          "ZipCode":"111344" ...       }, ...       "id": "1" ...    } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e039abdf5e889d7a5199509") } > db.getSpecificData.insertOne( ...    { ...       "StudentName": "Carol", ...       "Information": ... Read More

Advertisements