Found 1659 Articles for Big Data Analytics

Implementing MongoDB $exists and $ne?

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

281 Views

The $exists is used to check whethre a filed exists, whereas $ne is for not equal condition. Let us first create a collection with documents −> db.existsDemo.insertOne({"Name":"Chris", "Age":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3916d78f205348bc650") } > db.existsDemo.insertOne({"Name":"", "Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c39a6d78f205348bc651") } > db.existsDemo.insertOne({"Name":null, "Age":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3a66d78f205348bc652") } > db.existsDemo.insertOne({"Age":23}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c3c36d78f205348bc653") }Following is the query to display all documents from a collection with the help of find() method −> db.existsDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7c3916d78f205348bc650"),    "Name" : "Chris",    "Age" : 21 } { "_id" : ObjectId("5cd7c39a6d78f205348bc651"), "Name" : "", "Age" ... Read More

Use ObjectId under findOne() to fetch a specific record in MongoDB?

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

382 Views

Let us first create a collection with documents −> db.findOneWorkingDemo.insertOne({"ClientId":1, "ClientName":"Larry", "ClientAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c1716d78f205348bc64d") } > db.findOneWorkingDemo.insertOne({"ClientId":2, "ClientName":"Chris", "ClientAge":28}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c17d6d78f205348bc64e") } > db.findOneWorkingDemo.insertOne({"ClientId":3, "ClientName":"Robert", "ClientAge":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c1896d78f205348bc64f") }Following is the query to display all documents from a collection with the help of find() method −> db.findOneWorkingDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cd7c1716d78f205348bc64d"),    "ClientId" : 1,    "ClientName" : "Larry",    "ClientAge" : 26 } {    "_id" : ObjectId("5cd7c17d6d78f205348bc64e"),    "ClientId" : 2, ... Read More

How to calculate timestamp difference in hours with MongoDB?

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

793 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

634 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

Advertisements