Found 6702 Articles for Database

MongoDB shutdown option is unavailable? How to get it?

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

45 Views

You need to switch the database to admin. Following is the syntax −use adminThe syntax is as follows for shutdown option −db.shutdownServer()Let us implement the above syntax for shutdown −> use admin switched to db admin > db.shutdownServer()This will produce the following output −server should be down... 2019-04-22T19:11:40.949+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2019-04-22T19:11:42.197+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed

MongoDB query to match each element in a documents array to a condition?

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

165 Views

You can use every() in MongoDB for this. Let us create a collection with documents −> db.arrayConditionDemo.insertOne({"Name":"John", "Marks":[40, 43, 45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdbd06de8cc557214c0e1a") } > db.arrayConditionDemo.insertOne({"Name":"Mike", "Marks":[45]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdbd17de8cc557214c0e1b") } > db.arrayConditionDemo.insertOne({"Name":"Chris", "Marks":[43, 45, 59, 69, 78, 89]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5cbdbd3cde8cc557214c0e1c") }Display all documents from a collection with the help of find() method. The query is as follows −> db.arrayConditionDemo.find().pretty();This will produce the following output −{    "_id" : ObjectId("5cbdbd06de8cc557214c0e1a"),    "Name" : "John",    "Marks" : [       ... Read More

How can I use a script to create users in MongoDB?

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

247 Views

You can use createUser() method for this. Following is the syntax −db.createUser(    {       user: "yourUserName",       pwd: "yourPassword",       roles: [ { role: "read", db: "yourDatabaseName" } ]    } );Let us create a user in MongoDB. Here, we are using database ‘test’ −> db.createUser( ...    { ...       user: "David", ...       pwd: "David123456", ...       roles: [ { role: "read", db: "test" } ] ...    } ... );This will produce the following output −Successfully added user: {    "user" : "David",    "roles" : [       {          "role" : "read",          "db" : "test"       }    ] }

How to select only numeric strings in MongoDB?

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

423 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

244 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

502 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

203 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

307 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

Advertisements