AmitDiwan

AmitDiwan

8,390 Articles Published

Articles by AmitDiwan

Page 547 of 839

Display only the employee names with specific salaries in MongoDB documents with employee records?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 2K+ Views

To display only the employee names with specific salaries, set the salaries in MongoDB $in and fetch the names. Let us create a collection with documents> db.demo666.insertOne({"EmployeeName":"John", "EmployeeSalary":25000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c04824113ea5458c7d0d") } > db.demo666.insertOne({"EmployeeName":"Chris", "EmployeeSalary":35000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c05524113ea5458c7d0e") } > db.demo666.insertOne({"EmployeeName":"David", "EmployeeSalary":65000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c06024113ea5458c7d0f") } > db.demo666.insertOne({"EmployeeName":"Carol", "EmployeeSalary":40000}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1c06f24113ea5458c7d10") }Display all documents from a collection with the help of find() method −> db.demo666.find();This will produce the following output −{ "_id" : ObjectId("5ea1c04824113ea5458c7d0d"), "EmployeeName" ...

Read More

Searching for ranges in MongoDB?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 177 Views

To search for ranges, use sort() with limit(). Let us create a collection with documents −> db.demo665.insertOne({"Value":10}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1424113ea5458c7d08") } > db.demo665.insertOne({"Value":15}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1b24113ea5458c7d09") } > db.demo665.insertOne({"Value":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf1e24113ea5458c7d0a") } > db.demo665.insertOne({"Value":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf2324113ea5458c7d0b") } > db.demo665.insertOne({"Value":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1bf2b24113ea5458c7d0c") }Display all documents from a collection with the help of find() method −> db.demo665.find();This will produce the following output −{ "_id" : ObjectId("5ea1bf1424113ea5458c7d08"), "Value" : 10 ...

Read More

How to work with Stored JavaScript in MongoDB?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 489 Views

It is saved in the special system.js collection. For this, use db.system.js.save(). Following is the syntax −db.system.js.save({    _id: "anyFunctionName",    value: function (returnValue) {       return ‘yourMessage ' + returnValue;    } })Let us implement the above syntax. Following is the query −> db.system.js.save({ ...    _id: "returnValue", ...    value: function (data) { ...       return 'The value==== ' + data; ...    } ... }) WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Following is the query to call the above function to print the actual data −> db.eval("returnValue(20)") WARNING: db.eval is deprecatedThis will produce the following output −The value==== 20

Read More

Count number of rows in a MongoDB collection

AmitDiwan
AmitDiwan
Updated on 13-May-2020 889 Views

To count number of documents, use count() in MongoDB. Let us create a collection with documents −> db.demo664.insertOne({_id:1, ClientName:"Chris"}); { "acknowledged" : true, "insertedId" : 1 } > db.demo664.insertOne({_id:2, ClientName:"Bob"}); { "acknowledged" : true, "insertedId" : 2 } > db.demo664.insertOne({_id:3, ClientName:"Sam"}); { "acknowledged" : true, "insertedId" : 3 } > db.demo664.insertOne({_id:4, ClientName:"David"}); { "acknowledged" : true, "insertedId" : 4 }Display all documents from a collection with the help of find() method −> db.demo664.find();This will produce the following output −{ "_id" : 1, "ClientName" : "Chris" } { "_id" : 2, "ClientName" : "Bob" } { "_id" : 3, "ClientName" : ...

Read More

How would I filter out sub documents in MongoDB?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 284 Views

To filter out sub documents, use MongoDB aggregate and in that, use $unwind. Let us create a collection with documents −> db.demo662.insertOne( ... { ...    "details":[ ...   { ...       Name:"Chris", ...       Marks:35 ...    }, ...    { ...       Name:"Bob", ...       Marks:45 ...    }, ...    { ...       Name:"David", ...       Marks:30 ...    } ... ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1b2be24113ea5458c7d04") }Display all documents from a collection with the help of ...

Read More

MongoDB large collection and slow search? How to fix?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 474 Views

For faster search, create index. For this, use createIndex(). Let us create a collection with documents −> db.demo661.createIndex({ListOfName:1}); {    "createdCollectionAutomatically" : true,    "numIndexesBefore" : 1,    "numIndexesAfter" : 2,    "ok" : 1 } > db.demo661.insertOne({_id:1, ListOfName:["John", "Robert", "David"]}); { "acknowledged" : true, "insertedId" : 1 } > db.demo661.insertOne({_id:2, ListOfName:["Mike", "Sam"]}); { "acknowledged" : true, "insertedId" : 2 } > db.demo661.insertOne({_id:3, ListOfName:["John", "David", "Bob"]}); { "acknowledged" : true, "insertedId" : 3 }Display all documents from a collection with the help of find() method −> db.demo661.find();This will produce the following output −{ "_id" : 1, "ListOfName" : [ "John", ...

Read More

How to select maximum item in each group with MongoDB?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 428 Views

You can use $group. Let us create a collection with documents −> db.demo659.insertOne({Name:"Chris", CountryName:"US", "Marks":50}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cf9") } > db.demo659.insertOne({Name:"David", CountryName:"US", "Marks":60}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfa") } > db.demo659.insertOne({Name:"Mike", CountryName:"US", "Marks":55}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfb") } > db.demo659.insertOne({Name:"Chris", CountryName:"UK", "Marks":75}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfc") } > db.demo659.insertOne({Name:"David", CountryName:"UK", "Marks":54}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50724113ea5458c7cfd") } > db.demo659.insertOne({Name:"Mike", CountryName:"UK", "Marks":72}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea1a50824113ea5458c7cfe") }Display all documents from a collection ...

Read More

Push and slice multiple times in MongoDB?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 272 Views

To push and slice in MongoDB, use $push and $slice. Let us create a collection with documents −> db.demo656.insertOne({Name:"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea060264deddd72997713cf") }Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ "_id" : ObjectId("5ea060264deddd72997713cf"), "Name" : "John" }Here is the query to push and slice in MongoDB−> db.demo656.update({Name:"John"}, {"$push":{"ListOfName": {"$each": ["John"], "$slice": -9}}}); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Display all documents from a collection with the help of find() method −> db.demo656.find();This will produce the following output −{ ...

Read More

How to clear a MongoDB database?

AmitDiwan
AmitDiwan
Updated on 13-May-2020 2K+ Views

To clear, use dropDatabase. Following is the syntax −use yourDatabaseName; db.dropDatabase();To clear a MongoDB database, first show all the databases −> show dbsThis will produce the following output −MyDB    0.000GB admin    0.000GB config    0.000GB local    0.000GB onlinecustomertracker 0.000GB test    0.006GBNow, let us delete the database onlinecustomertracker −> use onlinecustomertracker switched to db onlinecustomertracker > db.dropDatabase(); { "dropped" : "onlinecustomertracker", "ok" : 1 }Following is the query to show all databases after deleting a database above −> show dbsThis will produce the following output −MyDB    0.000GB admin    0.000GB config    0.000GB local    0.000GB test    0.006GB

Read More

Using regex in MongoDB findOne()

AmitDiwan
AmitDiwan
Updated on 13-May-2020 686 Views

The findOne() returns one document that satisfies the specified query criteria on the collection. Let us create a collection with documents −> db.demo655.insertOne({subject:"MySQL"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050254deddd72997713cc") } > db.demo655.insertOne({subject:"MongoDB"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea0502b4deddd72997713cd") } > db.demo655.insertOne({subject:"Java"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5ea050314deddd72997713ce") }Display all documents from a collection with the help of find() method −> db.demo655.find();This will produce the following output −{ "_id" : ObjectId("5ea050254deddd72997713cc"), "subject" : "MySQL" } { "_id" : ObjectId("5ea0502b4deddd72997713cd"), "subject" : "MongoDB" } { "_id" : ObjectId("5ea050314deddd72997713ce"), "subject" : "Java" }Following is ...

Read More
Showing 5461–5470 of 8,390 articles
« Prev 1 545 546 547 548 549 839 Next »
Advertisements