Found 1659 Articles for Big Data Analytics

How do I work with array fields in MongoDB to match all?

AmitDiwan
Updated on 14-May-2020 09:22:26

158 Views

To match all in MongoDB, use $all. The $all operator selects the documents where the value of a field is an array that contains all the specified elements. Let us create a collection with documents −> db.demo695.insertOne({"ListOfValues":[100, 200, 500, 800]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4c4551299a9f98c938f") } > db.demo695.insertOne({"ListOfValues":[1000, 200, 4000]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea6d4cf551299a9f98c9390") }Display all documents from a collection with the help of find() method −> db.demo695.find();This will produce the following output −{ "_id" : ObjectId("5ea6d4c4551299a9f98c938f"), "ListOfValues" : [ 100, 200, 500, 800 ] } { "_id" : ObjectId("5ea6d4cf551299a9f98c9390"), "ListOfValues" ... Read More

How can I find documents in MongoDB based on the number of matched objects within an array?

AmitDiwan
Updated on 14-May-2020 09:20:21

93 Views

Let us see an example and create a collection with documents −> db.demo694.insertOne( ...    { ...       "details" : ...       [ ...          { ...             "Name" : "Chris", ...             Age:21 ...          }, ...          { ...             "Name" : "David", ...             Age:22 ...          } ...       ] ...    } ... ); {    "acknowledged" ... Read More

Ignore first 4 values in MongoDB documents and display the next 3?

AmitDiwan
Updated on 14-May-2020 09:16:27

79 Views

For this, use $slice and set thecount of values to be ignored and displayed. Let us create a collection with documents −> db.demo693.insertOne({Values:[10, 746, 736, 283, 7363, 424, 3535]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea58a04ece4e5779399c07b") } > db.demo693.insertOne({Values:[100, 200, 300, 100, 500, 700, 900, 30000, 40003, 45999]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea58a1eece4e5779399c07c") }Display all documents from a collection with the help of find() method −> db.demo693.find();This will produce the following output −{ "_id" : ObjectId("5ea58a04ece4e5779399c07b"), "Values" : [ 10, 746, 736, 283, 7363, 424, 3535 ] } { "_id" : ObjectId("5ea58a1eece4e5779399c07c"), "Values" : ... Read More

MongoDB query to find documents with specific FirstName and LastName

AmitDiwan
Updated on 14-May-2020 09:14:20

811 Views

To find documents with specific FirstName and LastName, use $and along with $in. Implement this in MongoDB find(). Let us create a collection with documents −> db.demo692.insertOne({FirstName:"Chris", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585dca7e81adc6a0b396a") } > db.demo692.insertOne({FirstName:"John", "LastName":"Brown"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585e2a7e81adc6a0b396b") } > db.demo692.insertOne({FirstName:"John", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585e7a7e81adc6a0b396c") } > db.demo692.insertOne({FirstName:"John", "LastName":"Doe"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585efa7e81adc6a0b396d") } > db.demo692.insertOne({FirstName:"Adam", "LastName":"Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea585faa7e81adc6a0b396e") }Display all documents from a collection with the help of find() ... Read More

MongoDB query to rename a collection?

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

94 Views

To rename a collection in MongoDB, use renameCollection(). Let us create a collection with documents −> db.demo690.insertOne({_id:101, Name:"Sam"}); { "acknowledged" : true, "insertedId" : 101 } > db.demo690.insertOne({_id:102, Name:"Mike"}); { "acknowledged" : true, "insertedId" : 102 } > db.demo690.insertOne({_id:103, Name:"John"}); { "acknowledged" : true, "insertedId" : 103 }Display all documents from a collection with the help of find() method −> db.demo690.find();This will produce the following output −{ "_id" : 101, "Name" : "Sam" } { "_id" : 102, "Name" : "Mike" } { "_id" : 103, "Name" : "John" }Following is the query to rename collection −> db.demo690.renameCollection("demo691"); { "ok" ... Read More

Writing a MongoDB insert statement for multiple insert at a time

AmitDiwan
Updated on 14-May-2020 09:10:04

174 Views

For multiple insert, use insert() in MongoDB. Let us create a collection with document −> db.demo689.insert([ ...    {ClientName:"Chris", "ClientAge":34, "ClientCountryName":"US"}, ...    {ClientName:"David", "ClientAge":28, "ClientCountryName":"UK"}, ...    {ClientName:"Bob", "ClientAge":39, "ClientCountryName":"AUS"}, ... ]); BulkWriteResult({    "writeErrors" : [ ],    "writeConcernErrors" : [ ],    "nInserted" : 3,    "nUpserted" : 0,    "nMatched" : 0,    "nModified" : 0,    "nRemoved" : 0,    "upserted" : [ ] })Display all documents from a collection with the help of find() method −> db.demo689.find();This will produce the following output −{ "_id" : ObjectId("5ea580dfa7e81adc6a0b3967"), "ClientName" : "Chris", "ClientAge" : 34, "ClientCountryName" : ... Read More

Set condition in MongoDB nested document?

AmitDiwan
Updated on 14-May-2020 09:04:56

277 Views

Let’s say we need to find a document with a value greater than specific value. For this, use dot notation in nested document and set the condition with $gt.Let us see an example and create a collection with documents −> db.demo688.insert( ... { ... information:{id:1, details:[ ...    {otherDetails:{ ...       values:75 ...       } ...    } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo688.insert({ ... information: ... { ... id:2, ... details: ... [ ...    {otherDetails:{ ...       values:78 ...    } ... } ... Read More

Accessing inner element of JSON array in MongoDB?

AmitDiwan
Updated on 14-May-2020 09:01:58

580 Views

To access inner element of JSON array in MongoDB, use dot notation. Let us create a collection with documents −> db.demo687.insert({CountryName:'US', ... info: ... { ... id:101, ... details: ... [ ... { ...    Name:'Chris', ...    SubjectName:'MongoDB', ...    otherDetails:{ ...       "Marks":58, ...       Age:23 ...    } ... } ... ] ... } ... } ... ) WriteResult({ "nInserted" : 1 }) > db.demo687.insert({CountryName:'UK', ... info: ... { ... id:102, ... details: ... [ ... { ...    Name:'David', ...    SubjectName:'MySQL', ...    otherDetails:{ ...       "Marks":78, ...   ... Read More

How to query MongoDB similar to “like” ?

AmitDiwan
Updated on 14-May-2020 08:59:59

296 Views

To implement similar to “like”, use find() along with // in MongoDB. Let us create a collection with documents −> db.demo686.insertOne({"FirstName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea55182a7e81adc6a0b395c") } > db.demo686.insertOne({"FirstName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea55186a7e81adc6a0b395d") } > db.demo686.insertOne({"FirstName":"ROBERT"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea5518fa7e81adc6a0b395e") } > db.demo686.insertOne({"FirstName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea55195a7e81adc6a0b395f") } > db.demo686.insertOne({"FirstName":"robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea5519ba7e81adc6a0b3960") }Display all documents from a collection with the help of find() method −> db.demo686.find();This will produce the following output −{ "_id" : ... Read More

MongoDB aggregation to fetch documents with specific field value?

AmitDiwan
Updated on 14-May-2020 09:03:57

576 Views

For this, use aggregate(). Let’s say we have to fetch documents with a field “Age” with value “21”.Let us now create a collection with documents −> db.demo685.insertOne( ...    { ...       "details": ...       [ ...          { ...             Name:"Chris", ...             Age:21 ...          }, ...          { ...             Name:"David", ...             Age:23 ...          }, ...       ... Read More

Advertisements