Python Operation MongoDB Database

Source: Internet
Author: User
Tags findone

About MongoDB

MongoDB is a document-based non-relational database (NoSQL), for example:

{"Foo":3, "greeting": "hello,world!"}

and case-sensitive, key value unique, non-repeatable, documents can be nested, key-value pairs are ordered

Collection

1, set is a set of documents

2, the document is similar to the row in the relational library

3, set similar to the table in the relational library

4, the collection of documents without a fixed structure (and the difference between the relational database)

Naming of collections

1. Cannot be an empty string

2. Cannot contain the \ s character (null character)

3. You cannot use the prefix of system.

4, the proposal does not contain the reserved word "$"

5. Splitting subsets of different namespaces (for example: blog.users)

Database

1. Multiple documents make up a collection, multiple collections make up a database

2. One instance can host multiple databases

3, each database has independent permissions

4. Reserved database name (admin,local,config)

Command line Operation MongoDB

Refer to the Novice tutorial

The \mongodb\server\3.4\bin enters the command-line window and then knocks MONGO into the MongoDB service, displaying all the databases

> show dbs;

Use a database (it will be created automatically if it does not exist)

>  use database name;
Inserting data

Write Data

> = {        name:'Jhon', age        :+, '        sex ': ' Male "}

Inserting data

> db. database name. Insert (Stu)
Querying data
> db. database name. FIND ()

Update data
>Stu_obj=Db.students.findOne () {"_id": ObjectId ("5a9a01c5b1489e70f3a89a0c"), "name": "Jhon", "Age": 
     +, "sex": "Male"}>Stu_obj.name="Jackchen" Jackchen>Db.students.Update({name: "Jhon"},stu_obj) Writeresult ({"nmatched":1, "nupserted":0, "nmodified":1 })>Db.students.findOne () {"_id": ObjectId ("5a9a01c5b1489e70f3a89a0c"), "name": "Jackchen", "Age" :  +, "sex": "Male"}

When updating the data, the second parameter in update completely replaces the previous parameter, so if the second parameter has only {name: "* *"}, the original data will be deleted

If you want to update multiple data, you can use the Updatemany

Summary: Updating data in this way is quite cumbersome and can be used with modifiers

Delete data

It turns out to be three data:

>, "Sex": "Male"}

Delete one of the following:

>1  })>, "sex": "Male"}

Delete all:

>2  })>  db.students.find ()>

Python Operation MongoDB Database

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.