Big Data Analytics Articles

Page 108 of 135

MongoDB query for Partial Object in an array

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 491 Views

Let us first create a collection with documents −> db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":1, "StudentName":"Chris"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfcf55bf3115999ed51206") } > db.queryForPartialObjectDemo.insertOne({_id:new ObjectId(), "StudentDetails": [{"StudentId":2, "StudentName":"David"}]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdfcf55bf3115999ed51207") }Following is the query to display all documents from a collection with the help of find() method −> db.queryForPartialObjectDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cdfcf55bf3115999ed51206"),    "StudentDetails" : [       {          "StudentId" : 1,          "StudentName" : "Chris"       }    ] } {    "_id" ...

Read More

Find the exact match in array without using the $elemMatch operator in MongoDB?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 349 Views

As an alternative, use the $eq operator. Let us first create a collection with documents −> db.equalDemo.insertOne({_id:1, "StudentFriendNames":["John", "Carol", "Sam"]}); { "acknowledged" : true, "insertedId" : 1 } > db.equalDemo.insertOne({_id:2, "StudentFriendNames":null}); { "acknowledged" : true, "insertedId" : 2 } > db.equalDemo.insertOne({_id:3, "StudentFriendNames":["Carol"]}); { "acknowledged" : true, "insertedId" : 3 } > db.equalDemo.insertOne({_id:4, "StudentFriendNames":["Sam"]}); { "acknowledged" : true, "insertedId" : 4 }Following is the query to display all documents from a collection with the help of find() method −> db.equalDemo.find();This will produce the following output −{ "_id" : 1, "StudentFriendNames" : [ "John", "Carol", "Sam" ] } { "_id" : 2, ...

Read More

How to use $regex in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 340 Views

Following is the syntax to use $regex in MongoDB −db.yourCollectionName.find({yourFieldName: { $regex: yourValue}});Let us first create a collection with documents −> db.regularExpressionDemo.insertOne({"UserName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc25bf3115999ed51210") } > db.regularExpressionDemo.insertOne({"UserName":"JOHN"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc2ebf3115999ed51211") } > db.regularExpressionDemo.insertOne({"UserName":"john"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc35bf3115999ed51212") } > db.regularExpressionDemo.insertOne({"UserName":"JoHn"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cdffc3ebf3115999ed51213") }Following is the query to display all documents from a collection with the help of find() method −> db.regularExpressionDemo.find();This will produce the following output −{ "_id" : ObjectId("5cdffc25bf3115999ed51210"), "UserName" : "John" } { ...

Read More

How to add an extra field in a sub document in MongoDB?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 591 Views

You can use update(). Let us first create a collection with documents −> db.addExtraFieldDemo.insertOne(    {       "_id": 1,       "UserName": "Larry" ,       "UserOtherDetails":[          {             "UserCountryName": "US",             "UserAge":23          },          {             "UserCountryName": "UK",             "UserAge":24          }       ]    } ); { "acknowledged" : true, "insertedId" : 1 }Following is the query to ...

Read More

How to get the matching document inside an array in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 191 Views

To get the matching document, use $elemMatch. Let us first create a collection with documents −> db.getMatchingDocumentDemo.insertOne(    {       _id :1,       "UserDetails":[          {             "UserName":"John",             "UserAge":23          }       ]    } ); { "acknowledged" : true, "insertedId" : 1 } > db.getMatchingDocumentDemo.insertOne( { _id :2, "UserDetails":[ { "UserName":"Larry", "UserAge":24 } ] } ); { "acknowledged" : true, "insertedId" : 2 }Following is the query to display all documents from a collection with the ...

Read More

MongoDB query to return specific fields from an array?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 728 Views

To return specific fields, use aggregate $project. Let us first create a collection with documents −> db.returnSpecificFieldDemo.insertOne(    {       "StudentId":1,       "StudentDetails": [          {             "StudentName":"Larry",             "StudentAge":21,             "StudentCountryName":"US"          },          {             "StudentName":"Chris",             "StudentAge":23,             "StudentCountryName":"AUS"          }       ]    } ); {    "acknowledged" ...

Read More

MongoDB query to replace value with aggregation?

Anvi Jain
Anvi Jain
Updated on 30-Jul-2019 536 Views

Use aggregate framework along with $literal operator. Let us first create a collection with documents −> db.replaceValueDemo.insertOne(    {       _id : 100,       "EmployeeName" :"Chris",       "EmployeeOtherDetails": {          "EmployeeDesignation" : "HR",          "EmployeeAge":27       }    } ); { "acknowledged" : true, "insertedId" : 100 } > db.replaceValueDemo.insertOne(    {       _id : 101,       "EmployeeName" :"David",       "EmployeeOtherDetails": {          "EmployeeDesignation" : "Tester",          "EmployeeAge":26       }    } ...

Read More

How to search for string or number in a field with MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 1K+ Views

You can use $in operator for this. Let us first create a collection with documents −> db.searchForStringOrNumberDemo.insertOne(    {       "_id": new ObjectId(),       "StudentName": "Larry",       "StudentDetails": {          "StudentMarks": {             "StudentMongoDBMarks": [44]          }       }    } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce2407c36e8b255a5eee944") } > db.searchForStringOrNumberDemo.insertOne( { "_id": new ObjectId(), "StudentName": "Larry", "StudentDetails": { "StudentMarks": { "StudentMongoDBMarks": ["44"] } } } ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce240f036e8b255a5eee945") }Following is ...

Read More

What is to be done when MongoDB takes too much time to find the record?

Smita Kapse
Smita Kapse
Updated on 30-Jul-2019 123 Views

To reduce the time to find record in MongoDB, you can use index. Following is the syntax −db.yourCollectionName.createIndex({yourFieldName:1});You can follow the below approaches to create index for field names based on number, text, hash, etc.First ApproachLet us create an index. Following is the query −> db.takeLessTimeToSearchDemo.createIndex({"EmployeeName":1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Second ApproachTo understand the above concept, let us create another index −> db.takeLessTimeToSearchDemo1.createIndex({"EmployeeName":"text"}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }Third ApproachLet us now create another index −> db.takeLessTimeToSearchDemo2.createIndex({"EmployeeName":"hashed"}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 }

Read More

How to update _id field in MongoDB?

Nishtha Thakur
Nishtha Thakur
Updated on 30-Jul-2019 381 Views

You can’t directly update _id field i.e. write some script to update. Let us first create a collection with documents −> db.updatingIdFieldDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ce271bb36e8b255a5eee949") }Following is the query to display all documents from a collection with the help of find() method −> db.updatingIdFieldDemo.find();This will produce the following output −{ "_id" : ObjectId("5ce271bb36e8b255a5eee949"), "StudentName" : "Chris" }Following is the query to update _id field in MongoDB −> var myDocument=db.updatingIdFieldDemo.findOne({StudentName:"Chris"}); > myDocument._id = 101; 101 > db.updatingIdFieldDemo.save(myDocument); WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : 101 }) > db.updatingIdFieldDemo.remove({_id:ObjectId("5ce271bb36e8b255a5eee949")}); WriteResult({ ...

Read More
Showing 1071–1080 of 1,347 articles
« Prev 1 106 107 108 109 110 135 Next »
Advertisements