Found 1357 Articles for MongoDB

How to drop a collection in MongoDB?

Daniol Thomas
Updated on 30-Jul-2019 22:30:25
To drop a collection in MongoDB, you need to use drop() command. The syntax is as follows:db.yourCollectionName.drop();The above syntax returns true or false. It returns true if the collection is dropped successfully otherwise false.Let us first display all collection names from MongoDB. Here, we have a database ‘sample’ that contains some collections. First you need to switch to the ‘sample’ database. The query is as follows:> use sample; switched to db sampleNow display all collection names with the help of show command. The query is as follows:> show collections;The following is the output:bookInformation userInformationAbove, you can see we have the ... Read More

How to create a new collection in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
To create a new collection in MongoDB, you need to use createCollection() method.Case 1: The easiest syntax to create a new collection in MongoDB is as follows:db.createCollection(“yourNewCollectionName”);Case 2: The alternative syntax to create a new collection in MongoDB is as follows:db.createCollections(“yourNewCollectionName”, options);The options parameter above can have the following values:capped field which returns boolean type which will either true or false.autoIndexId field which returns boolean type which will either true or false.size field which returns number.max field which returns a number also.From the above values, the autoIndexed is deprecated from MongoDB version 3.4 and 3.2. We are using MongoDB version ... Read More

How to drop a database in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
To drop a database in MongoDB, you need to use dropDatabase() method. The syntax is as follows:db.dropDatabase()The above syntax will delete the present working database. If you haven’t selected any database then it will delete the default database.Before deleting the database, first list all the databases with the help of show command. The query is as follows:> show dbs;The following is the output:admin 0.000GB config 0.000GB local 0.000GB sample 0.000GBHere, let us delete the database ‘sample’. To delete the database ‘sample’, you need to switch the database with the help of use command. The syntax is as follows:use yourDatabaseName;The query ... Read More

How to create a new database in MongoDB?

Nancy Den
Updated on 30-Jul-2019 22:30:25
To create a new database in MongoDB, you need to use the ‘use’ command. The syntax is as follows:use yourDatabaseName;The above syntax will create a new database in MongoDB. If the database name is already present then it will return the database name. Here, I am going to create a database with the name ‘sample’.The query to create a database in MongoDB is as follows -> use sample; switched to db sampleNow to check the present working database name you need to use the following syntax:dbThe query is as follows:> db sampleTo list all database names in MongoDB, you need ... Read More

Open Source Databases

Alex Onsman
Updated on 20-Jun-2020 08:17:31
Open source databases are those databases who have an open source code i.e anyone may view the code, study it or even modify it. Open source databases could be relational (SQL) or non relational (NoSQL).Why use Open Source Databases?It is quite expensive to create and maintain a database for any company. A huge chunk of the total software expenditure is used to handle databases. So, it is feasible to switch to low cost open source databases. This saves companies a lot of money in the long run.Open Source Databases in useThere many different open source databases in the market. All ... Read More

How to insert a Python object in Mongodb?

Rajendra Dharmkar
Updated on 16-Jun-2020 07:36:08
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo. For example, Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use ... Read More

How to prepare a Python date object to be inserted into MongoDB?

Rajendra Dharmkar
Updated on 12-Jun-2020 08:39:05
You can use the pymongo library in Python to connect to a MongoDB database and use it to insert, update, delete, etc objects in Python. The library supports Python datetime objects out of the box and you dont need to do anything special to insert dates in Mongo using PyMongo.Examplefrom pymongo import MongoClient # This will try to connect to MongoDB on the default port and host client = MongoClient() db = client.test_database # Insert the given dictionary to the objects collection: result = db.objects.insert_one({"last_modified": datetime.datetime.utcnow()}) print("Object inserted!")OutputThis will give the output −Object inserted!Note − Always use datetime.datetime.utcnow(), which returns ... Read More
Advertisements