Big Data Analytics Articles

Page 106 of 135

Projection result as an array of selected items in MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 188 Views

Use the distinct() for this, since it finds the distinct values for a specified field across a single collection or view and returns the results in an array.Let us first create a collection with documents −> db.projectionListDemo.insertOne({"_id":"1", "Subject":["MongoDB", "MySQL", "Java"]}); { "acknowledged" : true, "insertedId" : "1" } > db.projectionListDemo.insertOne({"_id":"2", "Subject":["MongoDB", "C", "C++"]}); { "acknowledged" : true, "insertedId" : "2" } > db.projectionListDemo.insertOne({"_id":"3", "Subject":["Java", "Python"]}); { "acknowledged" : true, "insertedId" : "3" }Display all documents from a collection with the help of find() method −> db.projectionListDemo.find().pretty();Output{ "_id" : "1", "Subject" : [ "MongoDB", "MySQL", "Java" ] } { "_id" : ...

Read More

MongoDB query to find data from an array inside an object?

Arjun Thakur
Arjun Thakur
Updated on 30-Jul-2019 564 Views

Let us first create a collection with documents −> db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"John",       "CustomerDetails" : {          "CountryName" : [             "AUS"          ],          "isMarried" : [             false          ]       }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefa5eeef71edecf6a1f6a5") } > db.findDataDemo.insertOne(    {       "_id": new ObjectId(),       "CustomerName":"Carol",       ...

Read More

MongoDB query to get last inserted document?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 14K+ Views

To get last inserted document, use sort() along with limit(1).Let us first create a collection with documents −> db.getLastInsertedDocument.insertOne({"Name":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb17eef71edecf6a1f6a8") } > db.getLastInsertedDocument.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb181ef71edecf6a1f6a9") } > db.getLastInsertedDocument.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb185ef71edecf6a1f6aa") }Display all documents from a collection with the help of find() method −> db.getLastInsertedDocument.find();Output{ "_id" : ObjectId("5cefb17eef71edecf6a1f6a8"), "Name" : "John" } { "_id" : ObjectId("5cefb181ef71edecf6a1f6a9"), "Name" : "Chris" } { "_id" : ObjectId("5cefb185ef71edecf6a1f6aa"), "Name" : "Robert" }Following is the query to get last inserted document −> db.getLastInsertedDocument.find({}).sort({_id:-1}).limit(1);Output{ "_id" ...

Read More

Display the last two values from field with MongoDB

George John
George John
Updated on 30-Jul-2019 123 Views

Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to get the last two values.Here, we have used -ve sign under $slice −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": -2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 1000, 98 ] }

Read More

Limit number of values in a field using MongoDB?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 394 Views

To limit number of values in a field, use $slice operator.Let us first create a collection with documents −> db.numberOfValuesDemo.insertOne({"Values":[100,200,300,900,1000,98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefb736ef71edecf6a1f6ab") }Display all documents from a collection with the help of find() method −> db.numberOfValuesDemo.find().pretty();Output{    "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"),    "Values" : [       100,       200,       300,       900,       1000,       98    ] }Following is the query to limit number of values in a field using MongoDB −> db.numberOfValuesDemo.find({},{ "Values": { "$slice": 2 } } );Output{ "_id" : ObjectId("5cefb736ef71edecf6a1f6ab"), "Values" : [ 100, 200 ] }

Read More

MongoDB query to select one field if the other is null and the first field if both are not null?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 475 Views

For this, use $ifNull operatorLet us first create a collection with documents −> dbquerySelectDemoinsertOne({"Value1":10, "Value2":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0ceef71edecf6a1f6b6") } > dbquerySelectDemoinsertOne({"Value1":null, "Value2":30}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0d7ef71edecf6a1f6b7") } > dbquerySelectDemoinsertOne({"Value1":60, "Value2":40}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefc0e2ef71edecf6a1f6b8") }Display all documents from a collection with the help of find() method −> dbquerySelectDemofind()pretty();Output{    "_id" : ObjectId("5cefc0ceef71edecf6a1f6b6"),    "Value1" : 10,    "Value2" : null } {    "_id" : ObjectId("5cefc0d7ef71edecf6a1f6b7"),    "Value1" : null,    "Value2" : 30 } {    "_id" : ObjectId("5cefc0e2ef71edecf6a1f6b8"),    "Value1" : 60, ...

Read More

Is it possible to return a list of specific values from a MongoDB query?

Chandu yadav
Chandu yadav
Updated on 30-Jul-2019 221 Views

Yes, using map(). Let us first create a collection with documents −> dblistOfSpecificValuesDemoinsertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc8fef71edecf6a1f6bb") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc94ef71edecf6a1f6bc") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc98ef71edecf6a1f6bd") } > dblistOfSpecificValuesDemoinsertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefcc9cef71edecf6a1f6be") }Following is the query to display all documents from a collection with the help of find() method −> dblistOfSpecificValuesDemofind();Output{ "_id" : ObjectId("5cefcc8fef71edecf6a1f6bb"), "StudentName" : "John" } { "_id" : ObjectId("5cefcc94ef71edecf6a1f6bc"), "StudentName" : "Chris" } { "_id" : ObjectId("5cefcc98ef71edecf6a1f6bd"), "StudentName" : "Robert" } ...

Read More

Update key value where another key equals some value in MongoDB?

George John
George John
Updated on 30-Jul-2019 266 Views

Use $elemMatch fir this with $setLet us first create a collection with documents −> dbkeyValueDemoinsertOne(    {       "_id" : new ObjectId(),       "CustomerDetails" : [          {             "Name" : "Chris",             "Age" :24,          },          {             "Name" : "Robert",             "Age" :29,          },          {             "Name" : "David",     ...

Read More

Specify return format in MongoDB to return the values as an array?

Ankith Reddy
Ankith Reddy
Updated on 30-Jul-2019 254 Views

Use aggregation for this and add the values to an array using the $group and $addToSet operatorLet us first create a collection with documents −> dbspecifyReturnFormatDemoinsertOne({"Subject":"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd364ef71edecf6a1f6c0") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd369ef71edecf6a1f6c1") } > dbspecifyReturnFormatDemoinsertOne({"Subject":"SQL Server"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cefd36fef71edecf6a1f6c2") }Following is the query to display all documents from a collection with the help of find() method −> dbspecifyReturnFormatDemofind();Output{ "_id" : ObjectId("5cefd364ef71edecf6a1f6c0"), "Subject" : "MongoDB" } { "_id" : ObjectId("5cefd369ef71edecf6a1f6c1"), "Subject" : "MySQL" } { "_id" : ObjectId("5cefd36fef71edecf6a1f6c2"), "Subject" : ...

Read More

Compare multiple properties in MongoDB?

George John
George John
Updated on 30-Jul-2019 207 Views

To compare multiple properties, use the $where operator. Let us first create a collection with documents −> dbcomparingMultiplePropertiesDemoinsertOne({"Values":[10, 70, 60]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cf228fcb64a577be5a2bc0a") }Following is the query to display all documents from a collection with the help of find() method −> dbcomparingMultiplePropertiesDemofind()pretty();This will produce the following document −{    "_id" : ObjectId("5cf228fcb64a577be5a2bc0a"),    "Values" : [       10,       70,       60    ] }Case 1: If condition becomes true then you will get an array otherwise nothing will get displayed Following is the query to compare multiple ...

Read More
Showing 1051–1060 of 1,345 articles
« Prev 1 104 105 106 107 108 135 Next »
Advertisements