Found 1661 Articles for Big Data Analytics

Check for null in MongoDB?

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

589 Views

We will use the Null type here. Following are the null types with the alias −TypeNumberAliasDouble1“double”String2“string”Object3“object”Array4“array”Binary data5“binData”Undefined6“undefined”ObjectId7“objectId”Boolean8“bool”Date9“date”Null10“null”Regular Expression11“regex”Following is the syntax for type 10 i.e. null −db.yourCollectionName.find({"yourFieldName":{ $type: 10 } });The above syntax will find only those documents which have null value. Let us first create a collection with documents −> db.mongoDbEqualDemo.insertOne({"Age":34}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9121a844af18acdffa3") } > db.mongoDbEqualDemo.insertOne({"Age":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9161a844af18acdffa4") } > db.mongoDbEqualDemo.insertOne({"Age":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e9191a844af18acdffa5") } > db.mongoDbEqualDemo.insertOne({"Age":56}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7e91e1a844af18acdffa6") } > db.mongoDbEqualDemo.insertOne({}); ... Read More

How can I drop a collection in MongoDB with two dashes in the name?

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

440 Views

Let us first see the syntax to drop a collection −db.getCollection("yourCollectionNameWithTwoDashes").drop();For demo, we will create a collection name with two dashes as shown below −> db.createCollection("company--EmployeeInformation"); { "ok" : 1 }Create the above collection “company--EmployeeInformation “ with documents. Following is the query:>db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Amazon", "EmployeeName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c5ff6d78f205348bc654") } >db.getCollection("company--EmployeeInformation").insertOne({"CompanyName":"Google", "EmployeeName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cd7c60b6d78f205348bc655") }Following is the query to display all documents from a collection with the help of find() method −> db.getCollection("company--EmployeeInformation").find();This will produce the following output −{ "_id" : ObjectId("5cd7c5ff6d78f205348bc654"), "CompanyName" : "Amazon", "EmployeeName" : "Chris" } { ... Read More

Implementing MongoDB $exists and $ne?

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

342 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

473 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

928 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

118 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

822 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

150 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

837 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

366 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

Advertisements