Giter VIP home page Giter VIP logo

mongodb's Introduction

mongoDb

MongoDB stores data in JSON-like documents, which makes the database very flexible and scalable. NoSQL database is MongoDB.

You can download a free MongoDB database at https://www.mongodb.com.

PyMongo Python needs a MongoDB driver to access the MongoDB database.

we will use the MongoDB driver "PyMongo".

We recommend that you use PIP to install "PyMongo".

pip3 install PyMongo

To test if the installation was successful, or if you already have "pymongo" installed, create a Python page with the following content:

import pymongo

To create a database in MongoDB, start by creating a MongoClient object, then specify a connection URL with the correct ip address and the name of the database you want to create.

MongoDB will create the database if it does not exist, and make a connection to it.


Important: In MongoDB, a database is not created until it gets content!

Important: In MongoDB, a collection is not created until it gets content!

MongoDB waits until you have inserted a document before it actually creates the collection.


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

mydb = myclient["mydatabase"]


You can check if a database exist by listing all databases in you system:

print(myclient.list_database_names())

================================================================================================================

To create a collection in MongoDB, use database object and specify the name of the collection you want to create.

MongoDB will create the collection if it does not exist.


import pymongo

myclient = pymongo.MongoClient("mongodb://localhost:27017/")

mydb = myclient["mydatabase"]

mycol = mydb["customers"]


Return a list of all collections in your database:

print(mydb.list_collection_names())

Insert Into Collection To insert a record, or document as it is called in MongoDB, into a collection,

we use the insert_one() method.

The first parameter of the insert_one() method is a dictionary containing the name(s) and value(s) of each field in the document you want to insert.


To insert multiple documents into a collection in MongoDB,

we use the insert_many() method.

The first parameter of the insert_many() method is a list containing dictionaries with the data you want to insert:


In MongoDB we use the find and findOne methods to find data in a collection.

Just like the SELECT statement is used to find data in a table in a MySQL database. To select data from a collection in MongoDB,

we can use the find_one() method.

The find_one() method returns the first occurrence in the selection.

The find() method returns all occurrences in the selection.

mongodb's People

Contributors

subhrapattnaik avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.