Difference Between NumberLong(x) and NumberLong(x) in MongoDB

AmitDiwan
Updated on 15-May-2020 07:45:55

316 Views

The NumberLong(x) goes beyond its limit value and round off the value whereas NumberLong(“x”) does not.Now, we will consider a number and would use it for both NumberLong(x) and NumberLong(“x”) to see the difference.Let us create a collection with documents −> db.demo603.insert({"longValue" : NumberLong(988998985857575789)}); WriteResult({ "nInserted" : 1 }) > db.demo603.insert({"longValueInString" : NumberLong("988998985857575789")});Display all documents from a collection with the help of find() method −> db.demo603.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e9605e5ed011c280a0905d1"),    "longValue" : NumberLong("988998985857575808") } {    "_id" : ObjectId("5e9605faed011c280a0905d2"),    "longValueInString" : NumberLong("988998985857575789") }

MongoDB Aggregation of Elements with Similar IDs in Different Documents

AmitDiwan
Updated on 15-May-2020 07:44:54

681 Views

For such grouping of documents, use $group in MongoDB aggregate(). Let us create a collection with documents −> db.demo602.insertOne({id:1, Name:"Chris"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960080ed011c280a0905c9") } > db.demo602.insertOne({id:2, Name:"David"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960086ed011c280a0905ca") } > db.demo602.insertOne({id:1, Name:"Bob"});{    "acknowledged" : true, "insertedId" : ObjectId("5e96008ced011c280a0905cb") } > db.demo602.insertOne({id:3, Name:"Mike"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960092ed011c280a0905cc") } > db.demo602.insertOne({id:2, Name:"John"});{    "acknowledged" : true, "insertedId" : ObjectId("5e960099ed011c280a0905cd") } > db.demo602.insertOne({id:1, Name:"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e9600a1ed011c280a0905ce") }Display all documents from a collection with the help of find() method −> db.demo602.find();This will produce the following ... Read More

Update in MongoDB and Prevent Overwrite

AmitDiwan
Updated on 15-May-2020 07:39:16

581 Views

Let us create a collection with documents −> db.demo601.insertOne( ...    { ...       id:1, ...       userDetails: ...          { ...             userName:"John", ...             userMailId:"John@gmail.com" ...          } ...       } ...    ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e95ff5ced011c280a0905c7") } > > db.demo601.insertOne( { id:2, userDetails: { userName:"Carol", userMailId:"Carol@gmail.com" } } );{    "acknowledged" : true,    "insertedId" : ObjectId("5e95ff71ed011c280a0905c8") }Display all documents from a collection with the help of find() method ... Read More

Create a New User and Set Role in MongoDB

AmitDiwan
Updated on 15-May-2020 07:12:18

503 Views

For this, use userAdminAnyDatabase since it gives all permission like admin. Following is the syntax −use admin db.createUser(    {       user: "yourUserName",       pwd: "yourPassword",       roles: [ { role: "yourRoleName", db: "yourDatabaseName" } ]    } )Let us implement the above syntax to create a user role. Following is the query to create a user with role "userAdminAnyDatabase" −> use admin switched to db admin > db.createUser( ...    { ...       user: "David Miller", ...       pwd: "123456", ...       roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] ...    } ... )This will produce the following output −Successfully added user: {    "user" : "David Miller",    "roles" : [       {          "role" : "userAdminAnyDatabase",          "db" : "admin"       }    ] }

Use Custom Variable While Updating a MongoDB Document

AmitDiwan
Updated on 15-May-2020 07:10:44

492 Views

To update, use update() and following is the syntax to create and use a sample custom variable −var anyVariableName=yourValue; db.yourCollectionName.update({filter}, {$set:{yourFieldName:yourVariableName}});Let us create a collection with documents −> db.demo600.insertOne({id:1, Name:"Robert"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a063f5f1e70e134e2699") } > db.demo600.insertOne({id:2, Name:"Mike"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a06bf5f1e70e134e269a") } > db.demo600.insertOne({id:3, Name:"Sam"});{    "acknowledged" : true, "insertedId" : ObjectId("5e94a072f5f1e70e134e269b") }Display all documents from a collection with the help of find() method −> db.demo600.find();This will produce the following output −{ "_id" : ObjectId("5e94a063f5f1e70e134e2699"), "id" : 1, "Name" : "Robert" } { "_id" : ObjectId("5e94a06bf5f1e70e134e269a"), "id" : 2, "Name" : "Mike" } ... Read More

Subtract Total Price Discount from Document Field Values in MongoDB

AmitDiwan
Updated on 15-May-2020 07:08:05

371 Views

To subtract values from document field values, use $subtract in MongoDB aggregate(). Let us create a collection with documents −> db.demo599.insertOne({"TotalPrice":250, "DiscountPrice":35});{    "acknowledged" : true, "insertedId" : ObjectId("5e948192f5f1e70e134e2696") } > db.demo599.insertOne({"TotalPrice":400, "DiscountPrice":10});{    "acknowledged" : true, "insertedId" : ObjectId("5e948199f5f1e70e134e2697") } > db.demo599.insertOne({"TotalPrice":1550, "DiscountPrice":50});{    "acknowledged" : true, "insertedId" : ObjectId("5e9481a0f5f1e70e134e2698") }Display all documents from a collection with the help of find() method −> db.demo599.find();This will produce the following output −{ "_id" : ObjectId("5e948192f5f1e70e134e2696"), "TotalPrice" : 250, "DiscountPrice" : 35 } { "_id" : ObjectId("5e948199f5f1e70e134e2697"), "TotalPrice" : 400, "DiscountPrice" : 10 } { "_id" : ObjectId("5e9481a0f5f1e70e134e2698"), "TotalPrice" : 1550, "DiscountPrice" ... Read More

Get Average from Document and Array Elements in MongoDB Aggregate

AmitDiwan
Updated on 15-May-2020 07:06:03

441 Views

For this, use $avg along with $group and aggregate(). Let us create a collection with documents −> db.demo598.insertOne( ...    { ...       Information:'Student', ...       id:100, ...       details:[ ...          {Name:'Chris', Marks:75}, ...          {Name:'Bob', Marks:55} ...       ] ...    } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e947fccf5f1e70e134e2694") } > db.demo598.insertOne( ...    { ...       Information:'Student', ...       id:101, ...       details:[ ...          {Name:'Chris', Marks:75}, ...     ... Read More

Find MongoDB Document Containing a Specific String

AmitDiwan
Updated on 15-May-2020 07:03:27

949 Views

To find which document contains a specific string, use $regex along with find(). Let us create a collection with documents −> db.demo597.insertOne({"Name":"John Doe"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947ae3f5f1e70e134e2690") } > db.demo597.insertOne({"Name":"John Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947ae8f5f1e70e134e2691") } > db.demo597.insertOne({"Name":"Chris Brown"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947aeff5f1e70e134e2692") } > db.demo597.insertOne({"Name":"Adam Smith"});{    "acknowledged" : true, "insertedId" : ObjectId("5e947afff5f1e70e134e2693") }Display all documents from a collection with the help of find() method −> db.demo597.find();This will produce the following output −{ "_id" : ObjectId("5e947ae3f5f1e70e134e2690"), "Name" : "John Doe" } { "_id" : ObjectId("5e947ae8f5f1e70e134e2691"), "Name" : "John Smith" } ... Read More

Retrieve a Single Document Specifying Criteria in MongoDB

AmitDiwan
Updated on 15-May-2020 07:02:14

404 Views

Use findOne() in MongoDB for this. The findOne() returns one document that satisfies the specified query criteria on the collection.Let us create a collection with documents −> db.demo596.insertOne({_id:1, "FirstName":"John", "LastName":"Smith"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo596.insertOne({_id:2, "FirstName":"John", "LastName":"Doe"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo596.insertOne({_id:3, "FirstName":"Chris", "LastName":"Brown"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo596.insertOne({_id:4, "FirstName":"David", "LastName":"Miller"}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo596.find();This will produce the following output −{ "_id" : 1, "FirstName" : "John", "LastName" ... Read More

MongoDB Query to Update Nested Document

AmitDiwan
Updated on 15-May-2020 07:01:00

2K+ Views

Let us create a collection with documents −> db.demo595.insertOne( { "Information": [    { "_id": new ObjectId(), Name:"Chris" },    { _id:new ObjectId(), Name:"Robert" } ] } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e93369cfd2d90c177b5bce4") }Display all documents from a collection with the help of find() method −> db.demo595.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e93369cfd2d90c177b5bce4"),    "Information" : [       {          "_id" : ObjectId("5e93369cfd2d90c177b5bce2"),          "Name" : "Chris"       },       {          "_id" : ObjectId("5e93369cfd2d90c177b5bce3"),     ... Read More

Advertisements