Found 1718 Questions for Big Data Analytics

Get Random record from MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
To get a random record from MongoDB, you can use aggregate function. The syntax is as follows:db.yourCollectionName.aggregate([{$sample:{size:1}}]);To understand the above syntax, let us create a collection with some documents. The query to create collection is as follows:>db.employeeInformation.insert({"EmployeeId":1, "EmployeeName":"Maxwell", "EmployeeAge":26}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":2, "EmployeeName":"David", "EmployeeAge":25}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":3, "EmployeeName":"Carol", "EmployeeAge":24}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":4, "EmployeeName":"Bob", "EmployeeAge":28}); WriteResult({ "nInserted" : 1 }) >db.employeeInformation.insert({"EmployeeId":5, "EmployeeName":"Sam", "EmployeeAge":27); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection with the help of find() method. The query is as follows:> db.employeeInformation.find().pretty();The following is the output:{ ... Read More

MongoDB query condition to compare two fields?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
You can use $where operator along with find() method to compare two fields in MongoDB. The syntax is as follows:db.yourCollectionName.find({$where:”yourCondition”}).pretty();To understand the above syntax, let us create a collection with some documents. The query to create a collection with documents is as follows:>db.compareTwoFields.insert({"Id":1, "FullName":{"FirstName":"John", "LastName":"Smith"}, "BranchName":"ComputerScience"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":2, "FullName":{"FirstName":"Smith", "LastName":"Smith"}, "BranchName":"Civil"}); WriteResult({ "nInserted" : 1 }) >db.compareTwoFields.insert({"Id":3, "FullName":{"FirstName":"David", "LastName":"Smith"}, "BranchName":"Mechanical"}); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection with the help of find() method. The query is as follows:> db.compareTwoFields.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c237568174aae23f5ef64"),   ... Read More

Get names of all keys in the MongoDB collection?

Nancy Den
Updated on 30-Jul-2019 22:30:25
The syntax to get names of all keys in the collection is as follows:var yourVariableName1=db.yourCollectionName.findOne(); for(var yourVariableName 2 in yourVariableName1) { print(yourVariableName); }To understand the above syntax, let us create a collection with documents. The collection name we are creating is “studentGetKeysDemo”.The following is the query to create documents:>db.studentGetKeysDemo.insert({"StudentId":1, "StudentName":"Larry", "StudentAge":23, "StudentAddress":"US", ... "StudentHobby":["Cricket", "Football", "ReadingNovel"],    "StudentMathMarks":89, "StudentDOB":ISODate('1998-04-06')});The following is the output:WriteResult({ "nInserted" : 1 })Display all documents from a collection with the help of find() method. The query is as follows:> db.studentGetKeysDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c12dd68174aae23f5ef5f"),    "StudentId" : 1,    "StudentName" : ... Read More

How to query MongoDB with “like”?

Nancy Den
Updated on 30-Jul-2019 22:30:25
You can easily query MongoDB with “like”:db.yourCollectionName.find({"yourFieldName" : /.*yourMatchingValue.*/}).pretty();To understand the above syntax, let us create a collection with some documents. Here, we have a collection with the name ‘employee’. The query is as follows:> db.employee.insert({"EmployeeName":"John", "EmployeeSalary":450000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Carol", "EmployeeSalary":150000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"James", "EmployeeSalary":550000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Jace", "EmployeeSalary":670000}); WriteResult({ "nInserted" : 1 }) > db.employee.insert({"EmployeeName":"Larry", "EmployeeSalary":1000000}); WriteResult({ "nInserted" : 1 })Now you can display all documents from a collection using find() method. The query is as follows:> db.employee.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c0b2e68174aae23f5ef59"),   ... Read More

Find objects between two dates in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
Use operator $gte and $lt to find objects between two dates in MongoDB. To understand these operators, let us create a collection.Creating a collection here:>db.order.insert({"OrderId":1, "OrderAddrees":"US", "OrderDateTime":ISODate("2019-02-19")}; WriteResult({ "nInserted" : 1 }) >db.order.insert({"OrderId":2, "OrderAddrees":"UK", "OrderDateTime":ISODate("2019-02-26")}; WriteResult({ "nInserted" : 1 })Display all documents from the collection with the help of find() method. The query is as follows:> db.order.find().pretty();The following is the output:{    "_id" : ObjectId("5c6c072068174aae23f5ef57"),    "OrderId" : 1,    "OrderAddrees" : "US",    "OrderDateTime" : ISODate("2019-02-19T00:00:00Z") } {    "_id" : ObjectId("5c6c073568174aae23f5ef58"),    "OrderId" : 2,    "OrderAddrees" : "UK",    "OrderDateTime" : ISODate("2019-02-26T00:00:00Z") }Here is the query ... Read More

Update MongoDB field using value of another field?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25
You can use aggregate function to update MongoDB field using the value of another field. Here, we will create two collections:namestudentInformation CollectionThe query to create first collection with documents is as follows:> db.name.insert({"FirstName":"John", "LastName":"Smith"}); WriteResult({ "nInserted" : 1 })Now you can display all documents from the collection with the help of find() method. The query is as follows:> db.name.find().pretty();The following is the output that displays the collection “name” documents:{    "_id" : ObjectId("5c6c00dd68174aae23f5ef55"),    "FirstName" : "John",    "LastName" : "Smith" } CollectionThe query to create second collection with documents is as follows:> db.studentInformation.insert({"StudentFirstName":"Carol", "StudentLastName":"Taylor"}); WriteResult({ "nInserted" : 1 })Now ... Read More

Retrieve only the queried element in an object array in MongoDB collection?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25
You can use projection operator $elemMatch to filter in queried element in an object array in MongoDB collection. To retrieve only the queried element in an object array in MongoDB, let us first create a collection with documents object array.The query is as follows:> db.objectArray.insert({"Persons":[    {"PersonName":"Adam", "PersonSalary":25000}, {"PersonName":"Larry", "PersonSalary":27000    }]});    WriteResult({ "nInserted" : 1 }) > db.objectArray.insert({"Persons":[    {"PersonName":"David", "PersonSalary":32000}, {"PersonName":"Carol", "PersonSalary":77000    }]});    WriteResult({ "nInserted" : 1 })Now you can display all the documents with the help of find(). The query is as follows:> db.objectArray.find().pretty();The following is the output:{    "_id" : ObjectId("5c6bfadc68174aae23f5ef53"),    "Persons" ... Read More

How to retrieve documents from a collection in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25
To retrieve documents from a collection in MongoDB, you need to use find() method. The syntax is as follows:db.yourCollectionName.find();The above syntax will return all the documents from a collection in MongoDB. To understand the above syntax, let us create a collection with documents. The query to create documents are as follows:> db.retrieveAllStudents.insertOne({"StudentId":"STUD101", "StudentName":"David", "StudentAge":24}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5cf68174aae23f5ef4e") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD102", "StudentName":"Carol", "StudentAge":22}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5e968174aae23f5ef4f") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD103", "StudentName":"Maxwell", "StudentAge":25}); {    "acknowledged" : true, "insertedId" : ObjectId("5c6bf5f768174aae23f5ef50") } > db.retrieveAllStudents.insertOne({"StudentId":"STUD104", "StudentName":"Bob", "StudentAge":23}); {    "acknowledged" : true, "insertedId" : ... Read More

How to delete all the documents from a collection in MongoDB?

Krantik Chavan
Updated on 30-Jul-2019 22:30:25
If you want to delete all documents from the collection, you can use deleteMany(). Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ab0e064f3d70fcc914805"),    "Name" : "Larry",    "Age" : 23 } {    "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"),    "Name" : "Mike",    "Age" : 21 } {    "_id" : ObjectId("5c6ab0f864f3d70fcc914807"),    "Name" ... Read More

How to delete document from a collection in MongoDB using deleteOne() method?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
To delete document from a collection in MongoDB, you can use the deleteOne() method. Let us first create a collection and insert some documents to it:> db.deleteDocumentsDemo.insert({"Name":"Larry", "Age":23}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Mike", "Age":21}); WriteResult({ "nInserted" : 1 }) > db.deleteDocumentsDemo.insert({"Name":"Sam", "Age":24}); WriteResult({ "nInserted" : 1 })Now display all the documents from the collection. The query is as follows:> db.deleteDocumentsDemo.find().pretty();The following is the output:{    "_id" : ObjectId("5c6ab0e064f3d70fcc914805"),    "Name" : "Larry",    "Age" : 23 } {    "_id" : ObjectId("5c6ab0ef64f3d70fcc914806"),    "Name" : "Mike",    "Age" : 21 } { "_id" : ObjectId("5c6ab0f864f3d70fcc914807"), "Name" : "Sam", ... Read More
Advertisements