MongoDB Simple operation

Source: Internet
Author: User
Tags mongodb

MongoDB is an open source database system based on distributed file storage.

in the case of high load , adding more nodes can guarantee the server performance

MongoDB stores data as a document and data Structures consist of key -value (key=value) pairs .

1.1(Linux)MongoDB Data directory exists in db directory under , we need to create our own initiative.

Mkdir-p/data/db (/datat/db is the default startup database path for MongoDB --dpath)

1.2 MongoDB Background management Shell

1.3 Viewing the current operation's document (database):db

1.4 insert a simple record and look for it:

>db.runoob.insert ({x:10})//Inserts the number into the runoob collection x Field

>db.runoob.find ()

2.1MongoDB Concept

2.2 Database

a MongoDB can build multiple databases.

The default database is "DB"and is stored in the data directory

The default database in MongoDB is test, and if you do not create a new database, the collection will be stored in the test Database

1) Show DBS can display a list of all data

2) use can be connected to a specified database

>use Local

Switched to Local

>db

Local

>

2.3 Documentation

A document is a set of key values (key-value)

Eg: {"site": "www.bird.com", "name": "bird"}

Note:1.the key/value in the document is ordered.

2, the value of the document can be not only double quotation marks inside the string, but also can be several other data types

3.MongoDB differentiated types and capitalization

4.MongoDB documents cannot have duplicate keys

5. The key of the document is a string. Except for a few exceptions, keys can use any UTF-8 character

2.4 Collection

Collection is A MongoDB document group, similar to a table in an RDBMS (relational database management system)

2.5MongoDB Data Types

3.1 Deleting a database

Syntax:db.dropdatabase ()// Delete current database, default to test

Instance:

1. Enter a database

>use Test

Switched to DB test

>

2. Execute the Delete command

>db.dropdatabase ()

{"Dropped": "Test", "OK": 1}

3.2 Deleting a collection

Syntax:db.collection.drop ()

Eg:>use Test

Switched to DB test

>show tables

Site

>db.site.drop

True

>show tables

>

3.3 Deleting Data

1. Delete " ban_friends_id": "BAN121113 " data

1 > Db.test_ttlsa_com.remove ({"ban_friends_id": "BAN121113"})

3.4 Delete all data in a table

Db. Table name . Remove ({})

4.1 inserting a document

syntax:db. Collection_name.insert (doucument)

1)eg: depositing documents in the Col Collection in the runoob Database

>db.runoob.insert ({title: ' MongoDB ',

Description: ' MongoDB is a nosql database ',

tags:[' MongoDB ', ' database ', ' NoSQL ']

likes:100

})

if the Col Collection is not in the database,MongoDB will automatically create this collection

2) View the inserted document

>db.runoob.find ()

4.2 define data as a variable

>document=({title= ' MongoDB ',

Description: ' MongoDB is a Nosql database ',

tags:[' MongoDB ', ' database ', ' NoSQL ']

likes:100

});

To perform an insert operation:

>db.col.insert (document)

Writeresult ({"ninserted": 1})

>

You can also use Db.clo.save (document)

5. Enquiry

5.1.1 less than pretty () method to display all documents in a formatted manner

Eg:db.access_points.find ({"admin_id": {$lt:). Pretty ()

5.1.2 less than or equal to

eg:db.access_points.find ({"admin_id": {$lte:). Pretty ()

5.1.3 greater than:

Eg:db.access_points.find ({"admin_id": {$gt: 50}})

5.1.4 greater than or equal to

Eg:db.access_points.find ({"admin_id": {$gle: 50}})

5.1.5 Not equal to

eg:db.access_points.find ({"admin_id": {$ne: +})

5.2 to re-query

Eg:db.access_controllers.distinct ("admin_id")

5.3 or or query

Grammar:

Db.status.find ({$ or : [{key1,value},{key2:value}]})

Eg:db.access_points ({$or: [{"admin_id": 1},{"Mac": "111111111111"}]}). Pretty ()

5.4 Skip () : Skips the number of record bars :

Eg:db.access_points.find (). Skip (2)// Skip section 2 data

5.5 sort ()

eg:db.access_controllers.find ({"Type": 1},{admin_id:1}). Sort ({admin_id:-1})//-1 descending; 1 Ascending

5.6 Db.col.find ({},{"title": 1,_id:0}). Limit (2)

Additional notes:

first one {} puts A Where condition, which returns all documents in the collection as null.

a second {} Specifies that those columns are displayed and not displayed (0 means that 1 is not displayed ).

5.6 Regular Query

1) query contains The contents of the string test

>db.posts.find ({post_text:{$regex: "Test"}})

2) query contains The contents of the character test

>db.posts.find ({post_text:/test/})

3) Query date contains 2017-09-06t01:33 string

Db.summary.find ({"Insert_time": {"$gte": Isodate (/.*2017-09-06t01:33.*/)}, "org_id": 1},{"Device": 1, "Insert_time" : 1})

MongoDB Simple operation

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.