Found 1659 Articles for Big Data Analytics

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

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

499 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

202 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

305 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

108 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

307 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

158 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

905 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

How to query MongoDB using the $ne operator?

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

93 Views

To query MongoDB using the $ne operator, following is the syntax −db.yourCollectionName.find({yourFieldName:{$ne:yourValue}}).pretty();Let us create a collection with documents −> db.notEqaulToDemo.insertOne({"StudentName":"Larry", "StudentMathMarks":68}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a6bde8cc557214c0ded") } > db.notEqaulToDemo.insertOne({"StudentName":"Chris", "StudentMathMarks":88}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a79de8cc557214c0dee") } > db.notEqaulToDemo.insertOne({"StudentName":"David", "StudentMathMarks":45}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a89de8cc557214c0def") } > db.notEqaulToDemo.insertOne({"StudentName":"Carol", "StudentMathMarks":69}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbd3a97de8cc557214c0df0") }Display all documents from a collection with the help of find() method −> db.notEqaulToDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbd3a6bde8cc557214c0ded"),    "StudentName" : "Larry",    "StudentMathMarks" : ... Read More

Unset an attribute from a single array element in MongoDB?

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

105 Views

Use $unset operator to unset an attribute. Let us first create a collection with documents −> db.unsetAnAttributeDemo.insertOne( ...    { ...       _id: 1, ...       "StudentDetails": [ ...          { ...             "StudentFirstName": "Ramit", ...             "StudentCountryName":"UK" ...          }, ...          { ...             "StudentFirstName": "Bob", ...             "StudentCountryName":"US" ...          }, ...          { ...       ... Read More

Advertisements