Found 1349 Articles for MongoDB

How to calculate timestamp difference in hours with MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

792 Views

To calculate timestamp difference, use aggregate framework. Let us first create a collection with documents −> db.timestampDifferenceDemo.insertOne({    "MovieBeginningTime": new ISODate("2019-05-12 10:20:30"),    "MovieEndingTime":new ISODate("2019-05-12 12:30:20") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ba1f6d78f205348bc644") } > db.timestampDifferenceDemo.insertOne({    "MovieBeginningTime": new ISODate("2019-05-12 04:00:00"),    "MovieEndingTime":new ISODate("2019-05-12 07:10:00") }); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ba3b6d78f205348bc645") }Following is the query to display all documents from a collection with the help of find() method −> db.timestampDifferenceDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7ba1f6d78f205348bc644"),    "MovieBeginningTime" : ISODate("2019-05-12T10:20:30Z"),    "MovieEndingTime" : ISODate("2019-05-12T12:30:20Z") } {    "_id" : ... Read More

Is there any way to see the MongoDB results in a better format?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

58 Views

Yes, you can use the findOne(). Following is the syntax −db.yourCollectionName.findOne();You can use toArray() as well −db.yourCollectionName.find().toArray();Let us first create a collection with documents −> db.betterFormatDemo.insertOne({"StudentName":"Adam Smith", "StudentScores":[98, 67, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ab826d78f205348bc640") } > db.betterFormatDemo.insertOne({"StudentName":"John Doe", "StudentScores":[67, 89, 56]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7ab936d78f205348bc641") } > db.betterFormatDemo.insertOne({"StudentName":"Sam Williams", "StudentScores":[45, 43, 33]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7aba76d78f205348bc642") }Following is the query to display all documents from a collection with the help of find() method −> db.betterFormatDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd7ab826d78f205348bc640"), "StudentName" ... Read More

Decrement only a single value in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

635 Views

Let us first create a collection with documents −>db.decrementingOperationDemo.insertOne({"ProductName":"Product-1", "ProductPrice":756}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a8ae6d78f205348bc63c") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-2", "ProductPrice":890}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a8b86d78f205348bc63d") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-3", "ProductPrice":994}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a8c66d78f205348bc63e") } >db.decrementingOperationDemo.insertOne({"ProductName":"Product-4", "ProductPrice":1000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a8d06d78f205348bc63f") }Following is the query to display all documents from a collection with the help of find() method −> db.decrementingOperationDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7a8ae6d78f205348bc63c"),    "ProductName" : "Product-1",    "ProductPrice" : 756 } {    "_id" : ObjectId("5cd7a8b86d78f205348bc63d"),    "ProductName" ... Read More

Search for documents matching first item in an array with MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

93 Views

Let us first create a collection with documents −> db.matchingFirstItemInTheArrayDemo.insertOne(    {       "ClientDetails": [          {             "ClientName": "Larry",             "ClientAge":28          }       ]    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a5d26d78f205348bc636") } > db.matchingFirstItemInTheArrayDemo.insertOne( {    "ClientDetails": [       {          "ClientName": "Chris",          "ClientAge":56,       }    ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7a5f56d78f205348bc637") } > db.matchingFirstItemInTheArrayDemo.insertOne( ... Read More

How to exclude _id without including other fields using the aggregation framework in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

632 Views

Let us first create a collection with documents −> db.excludeIdDemo.insertOne({"StudentFirstName":"John", "StudentAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701a56d78f205348bc632") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Robert", "StudentAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701af6d78f205348bc633") } > db.excludeIdDemo.insertOne({"StudentFirstName":"Chris", "StudentAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd701b86d78f205348bc634") }Following is the query to display all documents from a collection with the help of find() method −> db.excludeIdDemo.find();This will produce the following output −{ "_id" : ObjectId("5cd701a56d78f205348bc632"), "StudentFirstName" : "John", "StudentAge" : 21 } { "_id" : ObjectId("5cd701af6d78f205348bc633"), "StudentFirstName" : "Robert", "StudentAge" : 20 } { "_id" : ObjectId("5cd701b86d78f205348bc634"), "StudentFirstName" : "Chris", "StudentAge" ... Read More

Concatenate fields in MongoDB?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

273 Views

To concatenate fields, use the $concat operator. Let us first create a collection with documents −>db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Adam", "StudentLastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebf46d78f205348bc62e") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"John", "StudentLastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ebfc6d78f205348bc62f") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"David", "StudentLastName":"Miller"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec376d78f205348bc630") } >db.concatenateFieldsDemo.insertOne({"StudentFirstName":"Sam", "StudentLastName":"Williams"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ec436d78f205348bc631") }Following is the query to display all documents from a collection with the help of find() method −> db.concatenateFieldsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6ebf46d78f205348bc62e"),    "StudentFirstName" : "Adam",    "StudentLastName" : "Smith" } ... Read More

MongoDB query where all array items are less than a specified condition?

Smita Kapse
Updated on 30-Jul-2019 22:30:26

171 Views

Let us first create a collection with documents −> db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[89, 43, 32, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9e9f9b50a6c6dd317adb3") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[32, 33, 34, 40]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea13b50a6c6dd317adb4") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[45, 56, 66, 69]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea25b50a6c6dd317adb5") } > db.arrayElementsNotGreaterThanDemo.insertOne({"Scores":[46, 66, 77, 88]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd9ea3cb50a6c6dd317adb6") }Following is the query to display all documents from a collection with the help of find() method −> db.arrayElementsNotGreaterThanDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd9e9f9b50a6c6dd317adb3"),    "Scores" : [ ... Read More

MongoDB Query to search for records only in a specific hour?

Anvi Jain
Updated on 30-Jul-2019 22:30:26

448 Views

For this, use the $hour operator. Let us first create a collection with documents with one of the field as date −> db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-01-31 09:45:50")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8a86d78f205348bc62a") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-02-21 01:10:01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8b86d78f205348bc62b") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-04-01 04:10:11")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8e26d78f205348bc62c") } > db.mongoDbSearchForHoursDemo.insertOne({"CustomerName":"Larry", "OrderDatetime":new ISODate("2019-05-11 08:53:01")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6e8f26d78f205348bc62d") }Following is the query to display all documents from a collection with the help of find() method −> db.mongoDbSearchForHoursDemo.find().pretty();This will ... Read More

Update multiple rows in a single MongoDB query?

Nishtha Thakur
Updated on 30-Jul-2019 22:30:26

320 Views

Use the concept of initializeUnorderedBulkOp(). Let us first create a collection with documents −>db.upDateMultipleRowsDemo.insertOne({"CustomerName":"John", "CustomerPurchaseAmount":500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb06d78f205348bc626") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Chris", "CustomerPurchaseAmount":700}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb26d78f205348bc627") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"David", "CustomerPurchaseAmount":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb36d78f205348bc628") } >db.upDateMultipleRowsDemo.insertOne({"CustomerName":"Larry", "CustomerPurchaseAmount":1900}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6ceb46d78f205348bc629") }Following is the query to display all documents from a collection with the help of find() method −> db.upDateMultipleRowsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd6ceb06d78f205348bc626"),    "CustomerName" : "John",    "CustomerPurchaseAmount" : 500 } {   ... Read More

How to implement MongoDB $or operator

Smita Kapse
Updated on 30-Jul-2019 22:30:26

116 Views

Evaluate one or more expressions using the $or operator in MongoDB. Following is the syntax −db.yourCollectionName.find({ $or: [{ "yourFieldName": yourValue1 }, { "yourFieldName": yourValue2} ] } ).pretty();Let us first create a collection with documents −> db.orOperatorDemo.insertOne({"StudentNames":["John", "Carol", "Sam"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b80a6d78f205348bc61b") } > db.orOperatorDemo.insertOne({"StudentNames":["Robert", "Chris", "David"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b8266d78f205348bc61c") } > db.orOperatorDemo.insertOne({"StudentNames":["John"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd6b8346d78f205348bc61d") }Following is the query to display all documents from a collection with the help of find() method −> db.orOperatorDemo.find().pretty();This will produce the following output −{    "_id" ... Read More

Advertisements