Found 1349 Articles for MongoDB

How to query for records where field is null or not set in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

677 Views

Let us work around two cases −Case 1 − The syntax is as follows when the field is present and set to null.db.yourCollectionName.count({yourFieldName: null});Case 1 − The syntax is as follows when the field is not present and not set.db.yourCollectionName.count({yourFieldName: {$exists: false}});To understand both the above syntaxes, let us create a collection with the document. The query to create a collection with a document is as follows −> db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Larry", "EmployeeAge":null, "EmployeeSalary":18500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a995c6cea1f28b7aa07fe") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Bob", "EmployeeAge":21, "EmployeeSalary":23500}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a99836cea1f28b7aa07ff") } > db.fieldIsNullOrNotSetDemo.insertOne({"EmployeeName":"Carol", "EmployeeSalary":45500}); { ... Read More

Find items that do not have a certain field in MongoDB?

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

119 Views

To find items that do not have a certain field, use the $exists operator. The syntax is as follows −> db.yourCollectionName.find({"yourItemName":{$exists:false}}).pretty();To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDocumentDoNotHaveCertainFields.insertOne({"UserId":101, "UserName":"John", "UserAge":21}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a95fb6cea1f28b7aa07fb") } > db.findDocumentDoNotHaveCertainFields.insertOne({"UserName":"David", "UserAge":22, "UserFavouriteSubject":["C", "Java"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a96116cea1f28b7aa07fc") } > db.findDocumentDoNotHaveCertainFields.insertOne({"UserName":"Bob", "UserAge":24, "UserFavouriteSubject":["MongoDB", "MySQL"]}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a96306cea1f28b7aa07fd") }Display all documents from a collection with the help of find() method. The ... Read More

Performing regex Queries with PyMongo?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

887 Views

PyMongo is a Python distribution containing tools for working with MongoDB. To perform regex queries with PyMongo, the syntax is as follows −db.yourCollectionName.find({'yourCollectionName':{'$regex':'^yourWords'}}).pretty();The above syntax will give all those documents that start from a specific word.To understand the syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.performRegex.insertOne({"ClientName":"Larry", "ClientFolderInformation":[    "Folder 1", "Folder 2", "Folder 3", "Folder 4", "Folder 5"], "MainFolderLocation":"/MainFolder/Details/ClientFolder" }); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a8b186cea1f28b7aa07f2") } > db.performRegex.insertOne({"ClientName":"Larry", "ClientFolderInformation":[    "ClientFolder 1", "ClientFolder 2", "ClientFolder 3", "ClientFolder 4", "ClientFolder 5"],   ... Read More

Does MongoDB getUsers() and SHOW command fulfil the same purpose?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

68 Views

Both the getUsers() method and SHOW command can be used to list all users in the Mongo shell.Case 1 − Using getUsers()The syntax is as follows −db.getUsers();Case 2 − Using show commandThe syntax is as follows −show users;Let us implement both the syntaxes in order to list all users in the Mongo shell.Case 1 − The first query is as follows −> db.getUsers();The following is the output −[    {       "_id" : "test.John",       "user" : "John",       "db" : "test",       "roles" : [          {   ... Read More

How to stop MongoDB in a single command?

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

508 Views

In order to stop MongoDB in a single command, use the following syntax −mongo --eval "db.getSiblingDB('admin').shutdownServer()"Let us implement the above syntax in order to stop MongoDB in one command.First, use the shortcut key −Ctrl + C;The query is as follows −C:\Program Files\MongoDB\Server\4.0\bin>mongo --eval "db.getSiblingDB('admin').shutdownServer()"The following is the output − displaying the shutdown of the MongoDB server −MongoDB shell version v4.0.5 connecting to: mongodb://127.0.0.1:27017/?gssapiServiceName=mongodb Implicit session: session { "id" : UUID("c0337c02-7ee2-45d9-9349-b22d6b1ffe85") } MongoDB server version: 4.0.5 server should be down... 2019-03-14T21:56:10.327+0530 I NETWORK [js] trying reconnect to 127.0.0.1:27017 failed 2019-03-14T21:56:11.331+0530 I NETWORK [js] reconnect 127.0.0.1:27017 failed failed 2019-03-14T21:56:11.333+0530 I QUERY [js] ... Read More

How to list all databases in the Mongo shell?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

650 Views

To list all databases in the Mongo shell, you need to use show command. The syntax is as follows −show dbs;Let us implement the above syntax for MongoDB. The query is as follows −> show dbs;The following is the output −admin 0.000GB config 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBIf you create a new database in MongoDB then it won’t be present in list of databases.Let us check the same. Create a new database −> use studentTracker; switched to db studentTrackerNow list all the databases from Mongo shell. The query is as follows −> show dbs;The following is ... Read More

How to work Date query with ISODate in MongoDB?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

2K+ Views

Use $gte operator along with ISODate() to work Date query with ISODate in MongoDB.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.dateDemo.insertOne({"StudentName":"John", "StudentAge":26, "AdmissionDate":new ISODate("2013-06-07")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a65799064dcd4a68b70ea") }Display all documents from a collection with the help of find() method. The query is as follows −> db.dateDemo.find().pretty();The following is the output −{    "_id" : ObjectId("5c8a65799064dcd4a68b70ea"),    "StudentName" : "John",    "StudentAge" : 26,    "AdmissionDate" : ISODate("2013-06-07T00:00:00Z") }Here is the date query with ISODate in MongoDB ... Read More

How to find a document by the non-existence of a field in MongoDB?

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

65 Views

To find a document by the non-existence of a field in MongoDB, the syntax is as follows −db.yourCollectionName.find({ "yourFieldName" : { "$exists" : false } }).pretty();To understand the above syntax, let us create a collection with the document. The query to create a collection with a document is as follows −> db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"John", "StudentAge":25}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a5c629064dcd4a68b70e8") } > db.findDocumentNonExistenceFieldDemo.insertOne({"StudentName":"David", "StudentAge":26, "StudentMathMarks":78}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a5c809064dcd4a68b70e9") }Display all documents from a collection with the help of find() method. The query is as follows −> db.findDocumentNonExistenceFieldDemo.find().pretty();The following is the output −{ ... Read More

Return query based on date in MongoDB?

Anvi Jain
Updated on 30-Jul-2019 22:30:25

288 Views

To return query based on the date in MongoDB, let us take an example.To understand the concept, let us create a collection with the document. The query to create a collection with a document is as follows −> db.returnQueryFromDate.insertOne({"PassengerName":"John", "PassengerAge":23, "PassengerArrivalTime":new ISODate("2018-03-10 14:45:56")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57be9064dcd4a68b70e4") } > db.returnQueryFromDate.insertOne({"PassengerName":"Larry", "PassengerAge":21, "PassengerArrivalTime":new ISODate("2018-05-19 11:10:23")}); {    "acknowledged" : true,    "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e5") } > db.returnQueryFromDate.insertOne({"PassengerName":"Mike", "PassengerAge":24, "PassengerArrivalTime":new ISODate("2018-08-25 16:40:12")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e6") } >db.returnQueryFromDate.insertOne({"PassengerName":"Carol", "PassengerAge":26, "PassengerArrivalTime":new ISODate("2019-01-29 09:45:10")}); { "acknowledged" : true, "insertedId" : ObjectId("5c8a57bf9064dcd4a68b70e7") }Display all documents from a ... Read More

How to delete everything in a MongoDB database?

Smita Kapse
Updated on 30-Jul-2019 22:30:25

425 Views

You can delete everything in a MongoDB database using dropDatabase() function. The syntax is as follows −use yourDatabaseName; db.dropDatabase();The above syntax will delete everything in a MongoDB database.To delete everything in a MongoDB database, let us first display all the databases from MongoDB. The query is as follows −> show dbsThe following is the output −use yourDatabaseName; admin 0.000GB config 0.000GB flighInformation 0.000GB local 0.000GB sample 0.000GB sampleDemo 0.000GB test 0.003GBNow we will delete everything from the database ‘flightInformation’.First, you need to switch the database to ‘flightInformation’. The query is as follows −> use flighInformation; switched to db flighInformationNow here ... Read More

Advertisements