Found 1661 Articles for Big Data Analytics

Get MongoDB Databases in a JavaScript Array?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

240 Views

To get MongoDB databases in a JavaScript array, you can use runCommand(). Following is the query to get MongoDB databases in a JavaScript array> use admin; switched to db admin > allDatabasesDetails = db.runCommand({listDatabases: 1});This will produce the following output{    "databases" : [       {          "name" : "admin",          "sizeOnDisk" : 847872,          "empty" : false       },       {          "name" : "config",          "sizeOnDisk" : 98304,          "empty" : false     ... Read More

How to find a record by _id in MongoDB?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

661 Views

In order to find record by _id in MongoDB, you can use the following syntaxdb.yourCollectionName.find({"_id":yourObjectId});Let us create a collection with documents> db.findRecordByIdDemo.insertOne({"CustomerName":"Larry", "CustomerAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2c875e2eeda1d5c3671") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Bob", "CustomerAge":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2d175e2eeda1d5c3672") } > db.findRecordByIdDemo.insertOne({"CustomerName":"Carol", "CustomerAge":22}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2d775e2eeda1d5c3673") } > db.findRecordByIdDemo.insertOne({"CustomerName":"David", "CustomerAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dc2e375e2eeda1d5c3674") }Following is the query to display all documents from a collection with the help of find() method> db.findRecordByIdDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9dc2c875e2eeda1d5c3671"),    "CustomerName" ... Read More

How to improve querying field in MongoDB?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

138 Views

To improve querying field in MongoDB, you need to use index. Let us create a collection with documents> db.improveQueryDemo.insertOne( ... { ...    "PlayerDetails":[ ...       {"PlayerName": "John", "PlayerGameScore": 5690}, ...       {"PlayerName": "Carol", "PlayerGameScore": 2690}, ...    ] ... } ... ); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9dbaf875e2eeda1d5c3670") }Following is the query to display all documents from a collection with the help of find() method> db.improveQueryDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9dbaf875e2eeda1d5c3670"),    "PlayerDetails" : [       {          "PlayerName" : "John",     ... Read More

MongoDB equivalent of SELECT field AS `anothername`?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

2K+ Views

In MySQL, we give an alias name for a column. Similarly, you can give an alias name for field name in MongoDB. The MongoDB equivalent syntax is as followsdb.yourCollectionName.aggregate( [    { "$project": {       "_id": 0,       "anyAliasName": "$yourFieldName"    }} ]);Let us first create a collection with documents> db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d448827b86948e204ca91") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449027b86948e204ca92") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449527b86948e204ca93") } > db.selectFieldAsAnotherNameDemo.insertOne({"Name":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d449927b86948e204ca94") ... Read More

How to use $slice operator to get last element of array in MongoDB?

George John
Updated on 30-Jul-2019 22:30:25

881 Views

To get the last element of array in MongoDB, use the following syntaxdb.yourCollectionName.find({}, {yourArrayFieldName:{$slice:-1}});Let us first create a collection with documents>db.getLastElementOfArrayDemo.insertOne({"StudentName":"James", "StudentMathScore":[78, 68, 98]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d71a629b87623db1b2e") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Chris", "StudentMathScore":[88, 56, 34]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d83a629b87623db1b2f") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Larry", "StudentMathScore":[99]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2d8ea629b87623db1b30") } >db.getLastElementOfArrayDemo.insertOne({"StudentName":"Robert", "StudentMathScore":[90, 78, 67, 66, 75, 73]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2dada629b87623db1b31") }Following is the query to display all documents from a collection with the help of find() method> db.getLastElementOfArrayDemo.find().pretty();This will produce the following output{   ... Read More

Can I retrieve multiple documents from MongoDB by id?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

2K+ Views

Yes, to retrieve multiple docs from MongoDB by id, use the $in operator. The syntax is as followsdb.yourCollectionName.find({_id:{$in:[yourValue1, yourValue2, yourValue3, ...N]}});Let us first create a collection with documents:> db.retrieveMultipleDocsByIdDemo.insertOne({"_id":10, "CustomerName":"John"}); { "acknowledged" : true, "insertedId" : 10 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":14, "CustomerName":"Chris"}); { "acknowledged" : true, "insertedId" : 14 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":20, "CustomerName":"Robert"}); { "acknowledged" : true, "insertedId" : 20 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":25, "CustomerName":"Sam"}); { "acknowledged" : true, "insertedId" : 25 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":30, "CustomerName":"Bob"}); { "acknowledged" : true, "insertedId" : 30 } > db.retrieveMultipleDocsByIdDemo.insertOne({"_id":34, "CustomerName":"Carol"}); { "acknowledged" : true, "insertedId" : 34 }Following is the query to display all documents ... Read More

Insert to specific index for MongoDB array?

George John
Updated on 30-Jul-2019 22:30:25

533 Views

To insert a specific index for MongoDB array, you can use $push operator. Let us create a collection with documents>db.insertToSpecificIndexDemo.insertOne({"StudentName":"Larry", "StudentSubjects":["MySQL", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2562a629b87623db1b2c") } >db.insertToSpecificIndexDemo.insertOne({"StudentName":"Chris", "StudentSubjects":["C++", "C"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d2573a629b87623db1b2d") }Following is the query to display all documents from a collection with the help of find() method> db.insertToSpecificIndexDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9d2562a629b87623db1b2c"),    "StudentName" : "Larry",    "StudentSubjects" : [       "MySQL",       "Java"    ] } {    "_id" : ObjectId("5c9d2573a629b87623db1b2d"),    "StudentName" : "Chris",   ... Read More

MongoDB query which represents not equal to null or empty?

Chandu yadav
Updated on 30-Jul-2019 22:30:25

7K+ Views

To set a query for not equal to null or empty, use the $nin operator. The syntax is as followsdb.yourCollectionName.find({yourFieldName:{$nin:[null, ""]}});Let us create a collection with documents> db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Larry", "UserAge":24}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20b6a629b87623db1b26") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":29}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20bea629b87623db1b27") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Sam", "UserAge":32}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20c7a629b87623db1b28") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":null, "UserAge":27}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20d2a629b87623db1b29") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"Robert", "UserAge":26}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d20dda629b87623db1b2a") } > db.notEqualToNullOrEmptyDemo.insertOne({"UserName":"", "UserAge":23}); {    "acknowledged" : true,   ... Read More

How can I use MongoDB to find all documents which have a field, regardless of the value of that field?

Arjun Thakur
Updated on 30-Jul-2019 22:30:25

129 Views

To use MongoDB to find all documents which have a field, regardless of the value of that field, use the $exists operator. Following is the syntaxdb.yourCollectionName.find({yourFieldName:{$exists:true}});Let us create a collection with documents>db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"John", "StudentAge":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d60a629b87623db1b22") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Larry", "StudentAge":null}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d70a629b87623db1b23") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Chris", "StudentAge":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d7ba629b87623db1b24") } >db.findAllDocumentWhichHaveFieldDemo.insertOne({"StudentName":"Robert", "StudentAge":""}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c9d1d81a629b87623db1b25") }Following is the query to display all documents from a collection with the help of find() method> db.findAllDocumentWhichHaveFieldDemo.find().pretty();This will produce the following ... Read More

Inserting Date() in MongoDB through Mongo shell?

Ankith Reddy
Updated on 30-Jul-2019 22:30:25

1K+ Views

In order to insert Date() in MongoDB through Mongo shell, use the following syntaxvar yourVariableName= new Date(year, month, day, hour, minute); db.yourCollectionName({yourDateFieldName:yourVariableName});Let us first create a date variable> var creatingDate = new Date(2019, 03, 29, 13, 12);Let us create a collection with documents:>db.insertingDateUsingVariableDemo.insertOne({"UserName":"John", "UserMessages":["Hi", "Hello", "Awesome"], "UserPostDate":creatingDate});Following is the query to display all documents from a collection with the help of find() method> db.insertingDateUsingVariableDemo.find().pretty();This will produce the following output{    "_id" : ObjectId("5c9d1b19a629b87623db1b21"),    "UserName" : "John",    "UserMessages" : [       "Hi",       "Hello",       "Awesome"    ],    "UserPostDate" : ISODate("2019-04-29T07:42:00Z") }Read More

Advertisements