Database Articles

Page 238 of 546

Update MongoDB variable value with variable itself?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 550 Views

You cannot update a column value using itself. For this, you can use $set. Let us create a collection with documents −> db.demo256.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a3e91627c0c63e7dba8b") } > db.demo256.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a3ea1627c0c63e7dba8c") } > db.demo256.insertOne({"Name":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a3eb1627c0c63e7dba8d") }Display all documents from a collection with the help of find() method −> db.demo256.find();This will produce the following output −{ "_id" : ObjectId("5e47a3e91627c0c63e7dba8b"), "Name" : "Chris" } { "_id" : ObjectId("5e47a3ea1627c0c63e7dba8c"), "Name" : "Bob" } { "_id" : ObjectId("5e47a3eb1627c0c63e7dba8d"), "Name" : "David" }Following is ...

Read More

Split a string during MongoDB aggregate

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 434 Views

For this, use mapReduce(). Let us first create a collection with documents −> db.splitString.insertOne({"StudentName":"John Smith"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0849d925ddae1f53b62206") }Following is the query to display all documents from a collection with the help of find() method −> db.splitString.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e0849d925ddae1f53b62206"),    "StudentName" : "John Smith" }Here is the query to split a string −> db.splitString.mapReduce( ...    function() { ...       var StudentLastName = this.StudentName.split(/\s/).reverse()[0].toUpperCase(); ... ...       emit({ "StudentLastName": StudentLastName, "FirstObjectId": this._id }, this); ...    }, ...    function(){}, ...   ...

Read More

MongoDB bulk insert for documents

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 251 Views

For bulk insert, you can use insert() in MongoDB. Let us create a collection with documents −> var manyDocument = db.demo255.initializeUnorderedBulkOp(); > manyDocument.insert( { "Name":"Chris", Age:24} ); > manyDocument.insert( {"Name":"Bob", Age:22 } ); > manyDocument.insert( { "Name":"David", Age:23 } ); > manyDocument.execute(); 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.demo255.find();This will produce the following output −{ "_id" : ...

Read More

MongoDB query to 'sort' and display a specific number of values

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 162 Views

To sort in MongoDB, use sort(). For displaying only a specific number of values, use LIMIT. Let us create a collection with documents −> db.demo254.insertOne({"Name":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a0ab1627c0c63e7dba7f") } > db.demo254.insertOne({"Name":"Adam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a0b01627c0c63e7dba80") } > db.demo254.insertOne({"Name":"Bob"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e47a0b71627c0c63e7dba81") }Display all documents from a collection with the help of find() method −> db.demo254.find();This will produce the following output −{ "_id" : ObjectId("5e47a0ab1627c0c63e7dba7f"), "Name" : "Chris" } { "_id" : ObjectId("5e47a0b01627c0c63e7dba80"), "Name" : "Adam" } { "_id" : ObjectId("5e47a0b71627c0c63e7dba81"), "Name" : "Bob" ...

Read More

MongoDB query to change simple field into an object?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 258 Views

Fir this, you can use $rename. Let us first create a collection with documents −> db.changeSimpleFieldDemo.insertOne({"StudentMarks":58, "StudentSubject":"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e0847a825ddae1f53b62205") }Following is the query to display all documents from a collection with the help of find() method −> db.changeSimpleFieldDemo.find();This will produce the following output −{ "_id" : ObjectId("5e0847a825ddae1f53b62205"), "StudentMarks" : 58, "StudentSubject" : "MySQL" }Here is the query to change a field into an object. The “obj” field is a temporary field name we have used below −> db.changeSimpleFieldDemo.update({}, {$rename: {Student: 'obj'}}, {multi: true}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 ...

Read More

MongoDB query to fetch array values

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 553 Views

Use find() along with $elemMatch to fetch array values. Let us first create a collection with documents −> db.fetchingArrayValuesDemo.insertOne( ... { ...    "StudentName": "David", ...    "StudentDetails": [ ...       { ...          "FatherName": "Bob", ...          "CountryName": "US", ... ...          "Favourite": [ ...             { ...                "Teacher": "DAVID", ...                "Subject": [ ...                   "MySQL", ...         ...

Read More

MongoDB indexes not working when executing $elemMatch?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 579 Views

To implement indexes correctly with $elemMatch, you need to use the concept of explain(). Let us first create a collection with documents −> db.workingOfIndexesDemo.createIndex({"Information.StudentDetails.StudentName":1}, { sparse : true, background : true } ); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Chris"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f94825ddae1f53b621f7") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"David"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f94f25ddae1f53b621f8") } > db.workingOfIndexesDemo.insertOne({"Information":{"StudentDetails":{"StudentName":"Mike"}}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f95325ddae1f53b621f9") }Following is the query to display all documents from a collection with the help ...

Read More

Implement a MongoDB $cond field in a projection pipeline based on the presence or absence of a field?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 282 Views

For this, you can use $cond along with $anyElementTrue. NULL values (absence of a field) would evaluate to FALSE. With that, an empty array also returns FALSE with $ anyElementTrue.Let us first create a collection with documents −> db.presenceDemo.insertOne({"StudentName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f70c25ddae1f53b621f3") } > db.presenceDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71425ddae1f53b621f4") } > db.presenceDemo.insertOne({"StudentName":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71825ddae1f53b621f5") } > db.presenceDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f71e25ddae1f53b621f6") }Following is the query to display all documents from a collection with the help of find() ...

Read More

How can I search a collection to find a nested value in one of its documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 140 Views

For this, use double underscore( __) in find(). Let us first create a collection with documents −> db.nestedDemo.insertOne({"Information":{"__StudentName":"John Smith"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39125ddae1f53b621f0") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"John Doe"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f39e25ddae1f53b621f1") } > db.nestedDemo.insertOne({"Information":{"__StudentName":"Chris Brown"}}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06f3a625ddae1f53b621f2") }Following is the query to display all documents from a collection with the help of find() method −> db.nestedDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5e06f39125ddae1f53b621f0"),    "Information" : {       "__StudentName" : "John Smith"    } } {    "_id" ...

Read More

Find MongoDB document with array containing the maximum occurrence of a specific value

AmitDiwan
AmitDiwan
Updated on 31-Mar-2020 258 Views

For this, you can use aggregate(). Let us first create a collection with documents −> db.countOccurrencesDemo.insertOne({"ListOfValues":[65, 87, 89, 65, 67, 87, 87, 87]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06ef9325ddae1f53b621eb") } > db.countOccurrencesDemo.insertOne({"ListOfValues":[102, 65, 87, 65, 89, 65, 89, 65, 89, 65]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5e06efaa25ddae1f53b621ec") }Following is the query to display all documents from a collection with the help of find() method −> db.countOccurrencesDemo.find();This will produce the following output −{ "_id" : ObjectId("5e06ef9325ddae1f53b621eb"), "ListOfValues" : [ 65, 87, 89, 65, 67, 87, 87, 87 ] } { "_id" : ObjectId("5e06efaa25ddae1f53b621ec"), "ListOfValues" : ...

Read More
Showing 2371–2380 of 5,457 articles
« Prev 1 236 237 238 239 240 546 Next »
Advertisements