Found 1661 Articles for Big Data Analytics

How to select only numeric strings in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:26

550 Views

Let us create a collection with documents −> db.selectOnlyNumericDemo.insertOne({"UserId":"User101"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdb711de8cc557214c0e16") } > db.selectOnlyNumericDemo.insertOne({"UserId":"102"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdb716de8cc557214c0e17") } > db.selectOnlyNumericDemo.insertOne({"UserId":"User103"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdb71dde8cc557214c0e18") } > db.selectOnlyNumericDemo.insertOne({"UserId":"104"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdb725de8cc557214c0e19") }Display all documents from a collection with the help of find() method −> db.selectOnlyNumericDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbdb711de8cc557214c0e16"), "UserId" : "User101" } { "_id" : ObjectId("5cbdb716de8cc557214c0e17"), "UserId" : "102" } { "_id" : ObjectId("5cbdb71dde8cc557214c0e18"), "UserId" : "User103" } { "_id" : ... Read More

How can I update and increment two fields in one command in MongoDB?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:26

300 Views

Let us first create a collection with documents −> db.incrementDemo.insertOne({"Value1":10, "Value2":20}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdaf07de8cc557214c0e15") }Display all documents from a collection with the help of find() method. The query is as follows −> db.incrementDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbdaf07de8cc557214c0e15"),    "Value1" : 10,    "Value2" : 20 }Following is the query to increment two fields in one command in MongoDB −> db.incrementDemo.update({}, { $inc : { Value1 : 1, Value2 : 1 } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })Let us check both the fields ... Read More

How to find all objects created before specified Date in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:26

594 Views

You can use $lt operator for this. Let us create a collection with documents −> db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2016-03-21')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd91e4de8cc557214c0e0d") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2016-05-11')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd91ecde8cc557214c0e0e") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2017-01-31')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd91f9de8cc557214c0e0f") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2018-05-15')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd9206de8cc557214c0e10") } > db.beforeSpecifyDateDemo.insertOne({"UserLoginDate":new ISODate('2019-04-01')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd9211de8cc557214c0e11") }Display all documents from a collection with the help of find() method. The query is as follows −> db.beforeSpecifyDateDemo.find().pretty();This will produce the ... Read More

MongoDB query to remove item from array?

Samual Sam
Updated on 30-Jul-2019 22:30:25

252 Views

To remove item from array, you can use $pull operator. Let us create a collection with documents −> db.removeItemFromArray.insertOne(    { "_id":101, "StudentName":"Larry", "StudentSubjects":["C", "MongoDB", "Java", "MySQL"] } ); { "acknowledged" : true, "insertedId" : 101 }Display all documents from a collection with the help of find() method. The query is as follows −> db.removeItemFromArray.find().pretty();This will produce the following output −{    "_id" : 101,    "StudentName" : "Larry",    "StudentSubjects" : [       "C",       "MongoDB",       "Java",       "MySQL"    ] }Following is the query to remove item from an ... Read More

How to update record in MongoDB without replacing the existing fields?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

369 Views

You can use $set operator for this Let us first create a collection with documents −> db.updateRecordDemo.insertOne({"StudentName":"Larry"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f95de8cc557214c0e0a") } > db.updateRecordDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f9cde8cc557214c0e0b") } > db.updateRecordDemo.insertOne({"StudentName":"Mike"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6f9dde8cc557214c0e0c") }Display all documents from a collection with the help of find() method −> db.updateRecordDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbd6f95de8cc557214c0e0a"), "StudentName" : "Larry" } { "_id" : ObjectId("5cbd6f9cde8cc557214c0e0b"), "StudentName" : "David" } { "_id" : ObjectId("5cbd6f9dde8cc557214c0e0c"), "StudentName" : "Mike" }Following is the query to update record in ... Read More

How can I save new Date() in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

153 Views

To save new Date() in MongoDB, you can use new ISODate(). Let us create a collection with documents −> db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2018-01-31 12:34:56')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd67d4de8cc557214c0e04") } > db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2019-02-01 04:01:10')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd67e8de8cc557214c0e05") } > db.saveDateDemo.insertOne({"UserName":"John", "UserLoginDatetime":new ISODate('2019-04-22 12:36:45')}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6805de8cc557214c0e06") }Display all documents from a collection with the help of find() method −> db.saveDateDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd67d4de8cc557214c0e04"),    "UserName" : "John",    "UserLoginDatetime" : ISODate("2018-01-31T12:34:56Z") } {    "_id" : ObjectId("5cbd67e8de8cc557214c0e05"), ... Read More

Create array with MongoDB query?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

2K+ Views

You can use the concept of toArray() to create array. Following is the syntax −db.yourCollectonName.find({}, {yourFieldName:1}).toArray();Let us create a collection with documents −> db.createArrayDemo.insertOne({"UserName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6461de8cc557214c0e00") } > db.createArrayDemo.insertOne({"UserName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6467de8cc557214c0e01") } > db.createArrayDemo.insertOne({"UserName":"Robert"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd646cde8cc557214c0e02") } > db.createArrayDemo.insertOne({"UserName":"Sam"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6470de8cc557214c0e03") }Display all documents from a collection with the help of find() method −> db.createArrayDemo.find().pretty();This will produce the following output −{ "_id" : ObjectId("5cbd6461de8cc557214c0e00"), "UserName" : "Chris" } { "_id" : ObjectId("5cbd6467de8cc557214c0e01"), ... Read More

Count distinct value in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

397 Views

Use the concept of length to count distinct value. Following is the syntax −db.yourCollectionName.distinct("yourFieldName").length;Let us create a collection with documents −> db.countDistinctDemo.insertOne({"StudentName":"John"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6166de8cc557214c0dfa") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616ade8cc557214c0dfb") } > db.countDistinctDemo.insertOne({"StudentName":"Chris"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd616cde8cc557214c0dfc") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6170de8cc557214c0dfd") } > db.countDistinctDemo.insertOne({"StudentName":"David"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6175de8cc557214c0dfe") } > db.countDistinctDemo.insertOne({"StudentName":"Carol"}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd6181de8cc557214c0dff") }Display all documents from a collection with the help ... Read More

Get the count of the number of documents in a MongoDB Collection?

karthikeya Boyini
Updated on 30-Jul-2019 22:30:25

232 Views

To get the count of the number of documents in a collection MongoDB, you can use the below syntax −db.getCollectionNames().map(function(anyVariableName) {    return { "yourVariableName": yourVariableName, "count": db[yourVariableName].count() } });Here, we are using ‘test’ database.Let us implement the above syntax to get the count of the number of documents in a MongoDB collection −> db.getCollectionNames().map(function(ColName) { ... return { "ColName": ColName, "TotalDocument": db[ColName].count() } ... });This will produce the following output −[    {       "ColName" : "ConvertStringToDateDemo",       "TotalDocument" : 4    },    {       "ColName" : "Employee_Information",       "TotalDocument" ... Read More

Get index of given element in array field in MongoDB?

Samual Sam
Updated on 30-Jul-2019 22:30:25

1K+ Views

You can use $indexOfArray operator for this. Let us create a collection with documents −>db.getIndexDemo.insertOne({"InstructorName":"Chris", "InstructorSubject":["MongoDB", "MySQL", "Java", "C++"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd5251de8cc557214c0df8") }Display all documents from a collection with the help of find() method −> db.getIndexDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd5251de8cc557214c0df8"),    "InstructorName" : "Chris",    "InstructorSubject" : [       "MongoDB",       "MySQL",       "Java",       "C++"    ] }Following is the query to get index of given element in an array field in MongoDB −> db.getIndexDemo.aggregate( [ { "$project": { ... Read More

Advertisements