Found 1659 Articles for Big Data Analytics

How to remove primary key from MongoDB?

AmitDiwan
Updated on 11-May-2020 09:41:06

264 Views

To remove primary key in MongoDB, set _id value to 0 i.e. set the field you want to exclude as 0 in find(). Let us create a collection with documents −> db.demo471.insertOne({"ClientId":101, "ClientName":"Chris"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805711b0f3fa88e2279077") } > db.demo471.insertOne({"ClientId":102, "ClientName":"Bob"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80571db0f3fa88e2279078") } > db.demo471.insertOne({"ClientId":103, "ClientName":"David"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805724b0f3fa88e2279079") }Display all documents from a collection with the help of find() method −> db.demo471.find();This will produce the following output −{ "_id" : ObjectId("5e805711b0f3fa88e2279077"), "ClientId" : 101, "ClientName" : "Chris" } { "_id" : ObjectId("5e80571db0f3fa88e2279078"), "ClientId" ... Read More

Return all ages records that are ints with a MongoDB query

AmitDiwan
Updated on 11-May-2020 09:40:45

149 Views

To get all the ages that are ints from records having string and int ages records, use $type. The $type in MongoDB $type selects documents where the value of the field is an instance of the specified BSON type.Let us create a collection with documents −> db.demo470.insertOne({"Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805456b0f3fa88e2279070") } > db.demo470.insertOne({"Age":"Unknown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80545cb0f3fa88e2279071") } > db.demo470.insertOne({"Age":24});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805461b0f3fa88e2279072") } > db.demo470.insertOne({"Age":"Not provided"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80546bb0f3fa88e2279073") }Display all documents from a collection with the help of find() ... Read More

Find documents which contain a particular value in MongoDB using Regular Expressions?

AmitDiwan
Updated on 11-May-2020 09:38:50

42 Views

To find documents containing a particular value with Regular Expressions, use MongoDB $regex. Let us create a collection with documents −> db.demo469.insertOne({"StudentName":"John Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80532fb0f3fa88e227906b") } > db.demo469.insertOne({"StudentName":"Chris Brown"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80533ab0f3fa88e227906c") } > db.demo469.insertOne({"StudentName":"John Smith"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805341b0f3fa88e227906d") } > db.demo469.insertOne({"StudentName":"Jace Doe"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e805347b0f3fa88e227906e") } > db.demo469.insertOne({"StudentName":"David Miller"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e80534eb0f3fa88e227906f") }Display all documents from a collection with the help of find() method −> db.demo469.find();This will produce the following output −{ "_id" ... Read More

How do you find a MongoDB record that is two level deep?

AmitDiwan
Updated on 11-May-2020 09:38:28

223 Views

To find a MongoDB record that is two level deep, loop inside MongoDB $where. Let us create a collection with documents −> db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Chris", ...    "StudentAge" : 23 ... }, ... "SecondPosition" : { ...    "StudentName" : "David", ...    "StudentAge" : 20 ... } ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e804e2fb0f3fa88e2279069") } > db.demo468.insertOne( ... { ... "_id" : new ObjectId(), ... "FirstPosition" : { ...    "StudentName" : "Carol", ...    "StudentAge" : 21 ... ... Read More

How can I delete an item from an Object in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:35:56

613 Views

To delete an item from an object in MongoDB, use $unset. Let us create a collection with documents −> db.demo467.insertOne( ... { ... _id:101, ... "Information":{"Name":"Chris"} ... } ... ); { "acknowledged" : true, "insertedId" : 101 } > db.demo467.insertOne( ... { ... _id:102, ... "Information":{"Name":"David"} ... } ... ); { "acknowledged" : true, "insertedId" : 102 }Display all documents from a collection with the help of find() method −> db.demo467.find();This will produce the following output −{ "_id" : 101, "Information" : { "Name" : "Chris" } } { "_id" : 102, "Information" : { "Name" : "David" } }Following ... Read More

Using MongoDB nested $group and $sum to get the count of stocks with similar ProductID?

AmitDiwan
Updated on 11-May-2020 09:35:34

563 Views

The $group in MongoDB is used to group input documents by the specified _id expression. Let us create a collection with documents −> db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 1 ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e80477cb0f3fa88e2279066") } > > db.demo466.insertOne( ... { ... ... "ProductPrice" :150, ... "ProductQuantity" : 1, ... "ProductName" : "Product-1", ... "ActualAmount" :110, ... "ProductProfit" : 40, ... "ProductId" : 2 ... } ... ); {    "acknowledged" : true, ... Read More

How to get all docs which contain another doc in an array with MongoDB?

AmitDiwan
Updated on 11-May-2020 09:33:14

54 Views

For this, simply use dot notation with find() in MongoDB. Let us create a collection with documents −> db.demo465.insertOne( ... { ...    id: 101, ...    details: [{ ...       Name: "Chris", ...       Info: { ...          Subject: "MongoDB", ...          Marks:67 ...       } ...    }, { ...          Name: "David", ...          Info: { ...             Subject: "MySQL", ...             Marks:78 ...       } ... ... Read More

How to exclude array type field value in MongoDB?

AmitDiwan
Updated on 11-May-2020 09:31:01

278 Views

To exclude array type field value, use delete() in MongoDB. Let us create a collection with documents −> db.demo464.insertOne( ... { ... ...    "id" : "101", ...    "details": [ ...       { ...          Name:"Chris" ...       }, ...       { ...          Name:"David" ...       } ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e7f8832cb66ccba22cc9dda") }Display all documents from a collection with the help of find() method −> db.demo464.find();This will produce the following output −{ "_id" ... Read More

Retrieve data from a MongoDB collection?

AmitDiwan
Updated on 11-May-2020 09:30:22

539 Views

To return a single document from a collection, use findOne() in MongoDB. Let us create a collection with documents −> db.demo463.insertOne({"StudentName":"Chris Brown", "StudentAge":21, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ec8cb66ccba22cc9dcf") } > db.demo463.insertOne({"StudentName":"David Miller", "StudentAge":23, "StudentCountryName":"UK"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ed5cb66ccba22cc9dd0") } > db.demo463.insertOne({"StudentName":"John Doe", "StudentAge":22, "StudentCountryName":"AUS"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7ee1cb66ccba22cc9dd1") } > db.demo463.insertOne({"StudentName":"John Smith", "StudentAge":24, "StudentCountryName":"US"});{    "acknowledged" : true,    "insertedId" : ObjectId("5e7f7eefcb66ccba22cc9dd2") }Display all documents from a collection with the help of find() method −> db.demo463.find();This will produce the following output −{ "_id" : ObjectId("5e7f7ec8cb66ccba22cc9dcf"), "StudentName" : "Chris ... Read More

Update a specific MongoDB document in array with $set and positional $ operator?

AmitDiwan
Updated on 11-May-2020 09:28:57

191 Views

To update a specific document in array with $set and positional $ operator, use MongoDB updateOne(). The updateOne() updates a single document in a collection based on a query filter.Let us create a collection with documents −> db.demo462.insertOne( ... { ...    "id":1, ...    "DueDateDetails": [ ...       { ...          "Name": "David", ...          "Age":21, ...          "CountryName":["US", "UK"] ...       }, ...       { ... ...          "Name": "Chris", ...          "Age":23, ...       ... Read More

Advertisements