Found 1659 Articles for Big Data Analytics

How do I sort natural in MongoDB?

AmitDiwan
Updated on 14-May-2020 08:52:35

622 Views

Use $natural to sort natural in MongoDB. Let us create a collection with documents −> db.demo684.insertOne({Value:10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530cea7e81adc6a0b3957") } > db.demo684.insertOne({Value:50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530d1a7e81adc6a0b3958") } > db.demo684.insertOne({Value:60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530d4a7e81adc6a0b3959") } > db.demo684.insertOne({Value:40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea530d8a7e81adc6a0b395a") }Display all documents from a collection with the help of find() method −> db.demo684.find();This will produce the following output −{ "_id" : ObjectId("5ea530cea7e81adc6a0b3957"), "Value" : 10 } { "_id" : ObjectId("5ea530d1a7e81adc6a0b3958"), "Value" : 50 } { "_id" : ObjectId("5ea530d4a7e81adc6a0b3959"), ... Read More

What happens when we try to add a number to undefined value?

AmitDiwan
Updated on 14-May-2020 08:40:21

585 Views

If you try to add a number to undefined value then you will get a NaN. The NaN defines Not a Number. Following is an example −Case 1var anyVar=10+undefined; print(anyVar) //Result will be NaNCase 2var anyVar1=10; var anyVar2; var anyVar=yourVar1+yourVar2; print(anyVar) //Result will be NaNCase 1Let us implement the above cases. The query is as follows −> var result=10+undefined; > print(result);This will produce the following output −NaNCase 2Let us implement the above case −> var value; > var value1=10; > var result=value1+value > resultThis will produce the following output −NaN

Count the documents with a field value beginning with 13

AmitDiwan
Updated on 14-May-2020 08:39:01

61 Views

To count the documents, use $count. For values beginning with 13, use $regex. You can use $regex. Let us create a collection with documents −> db.demo570.insertOne({Information:{Value:"13675"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e90959b39cfeaaf0b97b583") } > db.demo570.insertOne({Information:{Value:"14135"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e9095a739cfeaaf0b97b584") } > db.demo570.insertOne({Information:{Value:"15113"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e9095b639cfeaaf0b97b585") } > db.demo570.insertOne({Information:{Value:"13141"}});{    "acknowledged" : true, "insertedId" : ObjectId("5e9095c139cfeaaf0b97b586") }Display all documents from a collection with the help of find() method −> db.demo570.find();This will produce the following output −{ "_id" : ObjectId("5e90959b39cfeaaf0b97b583"), "Information" : { "Value" : "13675" } } { "_id" : ObjectId("5e9095a739cfeaaf0b97b584"), "Information" : ... Read More

MongoDB query to match documents whose _id is in an array as part of a subdocument?

AmitDiwan
Updated on 14-May-2020 08:37:07

150 Views

Let us create a collection with documents −> db.demo568.insertOne({ _id: 101, details: [ {id : 101 }, { id:103 } ] }); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method −> db.demo568.find();This will produce the following output −{ "_id" : 101, "details" : [ { "id" : 101 }, { "id" : 103 } ] } Following is the query to create second collection: > db.demo569.insertOne({ _id: 101, details: "John" }) { "acknowledged" : true, "insertedId" : 101 } > db.demo569.insertOne({ _id: 102, details: "Chris" }) { "acknowledged" : ... Read More

MongoDB collection query to exclude some fields in find()?

AmitDiwan
Updated on 14-May-2020 08:35:46

8K+ Views

Set the fields you don’t want to include as 0 as in the below syntax. Here, we have set fields “yourFieldName1” and “yourFieldName2” as 0 −db.yourCollectionName.find(yourQuery, {yourFieldName1:0, yourFieldName2:0});To understand the above syntax, let us create a collection with documents −> db.demo567.insertOne({"Name":"Chris", Age:21});{    "acknowledged" : true, "insertedId" : ObjectId("5e908fa139cfeaaf0b97b57b") } > db.demo567.insertOne({"Name":"David", Age:23});{    "acknowledged" : true, "insertedId" : ObjectId("5e908fa939cfeaaf0b97b57c") } > db.demo567.insertOne({"Name":"Bob", Age:22});{    "acknowledged" : true, "insertedId" : ObjectId("5e908faf39cfeaaf0b97b57d") } > db.demo567.insertOne({"Name":"Carol", Age:20});{    "acknowledged" : true, "insertedId" : ObjectId("5e908fb539cfeaaf0b97b57e") }Display all documents from a collection with the help of find() method −> db.demo567.find();This will produce the following ... Read More

Grouping the array items in MongoDB and get the count the products with similar price?

AmitDiwan
Updated on 14-May-2020 08:33:48

829 Views

To group the array items, use $group along with $sort. Let us create a collection with documents −> db.demo566.insertOne( ... { ... ...    "ProductInformation" : [ ...       { ...          "ProductName" : "Product-1", ...          "ProductPrice" :100 ...       }, ...       { ...          "ProductName" : "Product-2", ...          "ProductPrice" :1100 ...       }, ...       { ...          "ProductName" : "Product-3", ...          "ProductPrice" :100 ...     ... Read More

MongoDB - how can I access fields in a document?

AmitDiwan
Updated on 14-May-2020 08:31:37

319 Views

To access fields in a document, simply use find(). Let us create a collection with documents −> db.demo565.insertOne( ... { ...    id:101, ...    Name:"David", ...    "CountryName":"US" ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e90896739cfeaaf0b97b577") } > > db.demo565.insertOne( ... { ...    id:102, ...    Name:"Carol", ...    "CountryName":"UK" ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e90896839cfeaaf0b97b578") } > > db.demo565.insertOne( ... { ...    id:103, ...    Name:"Sam", ...    "CountryName":"AUS" ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5e90896839cfeaaf0b97b579") }Display all ... Read More

Make MongoDB replace single array value with string?

AmitDiwan
Updated on 14-May-2020 08:29:49

551 Views

To replace, use $set and positional($) operator. Let us create a collection with documents −> db.demo564.insertOne({"StudentName":["Chris", "David", "Mike", "Sam"]});{    "acknowledged" : true, "insertedId" : ObjectId("5e90880a39cfeaaf0b97b576") }Display all documents from a collection with the help of find() method −> db.demo564.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e90880a39cfeaaf0b97b576"),    "StudentName" : [       "Chris",       "David",       "Mike",       "Sam"    ] }Following is the query to replace single array value with string −> db.demo564.updateMany( ...    { "StudentName": "David" }, ...    { "$set": { "StudentName.$": "Carol Taylor" } ... Read More

How to get items with a specific value from documents using MongoDB shell?

AmitDiwan
Updated on 14-May-2020 08:28:12

416 Views

To get items with a specific value, simply use find(). Let us create a collection with documents −> db.demo563.insertOne({"Name":"Chris", "Age":21, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f546c54b4472ed3e8e878") } > db.demo563.insertOne({"Name":"Bob", "Age":23, "isMarried":false}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f547854b4472ed3e8e879") } > db.demo563.insertOne({"Name":"Carol", "Age":23, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f548b54b4472ed3e8e87a") } > db.demo563.insertOne({"Name":"Mike", "Age":21, "isMarried":true}){    "acknowledged" : true, "insertedId" : ObjectId("5e8f549454b4472ed3e8e87b") }Display all documents from a collection with the help of find() method −> db.demo563.find();This will produce the following output −{ "_id" : ObjectId("5e8f546c54b4472ed3e8e878"), "Name" : "Chris", "Age" : 21, "isMarried" : true } { "_id" : ... Read More

How to search for a record (field) and then delete it in MongoDB?

AmitDiwan
Updated on 14-May-2020 08:26:23

418 Views

To search for a field, use $exists and to delete it, use $unset. The $unset operator in MongoDB deletes a particular field.Let us create a collection with documents −> db.demo562.insertOne({"Name":"Chris", "Age":21});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae854b4472ed3e8e872") } > db.demo562.insertOne({"Age":20});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4ae954b4472ed3e8e873") } > db.demo562.insertOne({"Name":"David", "Age":23});{    "acknowledged" : true,    "insertedId" : ObjectId("5e8f4aea54b4472ed3e8e874") }Display all documents from a collection with the help of find() method −> db.demo562.find();This will produce the following output −{ "_id" : ObjectId("5e8f4ae854b4472ed3e8e872"), "Name" : "Chris", "Age" : 21 } { "_id" : ObjectId("5e8f4ae954b4472ed3e8e873"), "Age" : 20 } ... Read More

Advertisements