Basic operations of mongodb and basic operations of mongodb

Source: Internet
Author: User
Tags install mongodb

Basic operations of mongodb and basic operations of mongodb

Install mongodb in http://blog.csdn.net/u011575570/article/details/47791135
Including linux and Windows
The following are some basic operations:
/Usr/local/mongodb/bin/mongo # Start mongodb
MongoDB shell version: 2.2.6
Connecting to: test

Show dbs; # view Databases
Imooc 0.203125 GB
Local (empty)
Use imooc_2 # When there is no imooc_2 database, this command is equivalent to creating this database.
Switched to db imooc_2
Show tables # You can also use the show collections set to View tables in the database.
System. indexes
Teacher

Db. teacher. insert ({'name': lilaoshi, age: 24}) # When the imooc_2 database does not have a teacher table, this command is equivalent to creating the table and inserting data
Sat Aug 29 20:54:09 ReferenceError: lilaoshi is not defined (shell): 1 # Because lilaoshi is a string
Db. teacher. insert ({'name': 'lilaoshi ', age: 24}) # insert
Db. teacher. find (); # search (_ id is the default key of mongodb)
{"_ Id": ObjectId ("55e1ab7ab6aebb3a7ca49928"), "name": "lilaoshi", "age": 24}

Use imooc # access the imooc Database
Switched to db imooc
Show tables; # View tables in the database
Goods
Imooc_collection
Stu
System. indexes
Tea
Db. stu. count () # View data volume
10000
Db. stu. find ({_ id: 3 })
{"_ Id": 3, "sn": "003", "name": "xiaoming3 "}
Db. stu. remove ({_ id: 3}) # delete data by _ id
Db. stu. find ({_ id: 3 })
Db. stu. remove () # delete all data
Db. stu. find ()
Db. stu. insert ({_ id: 2, 'name': 'xiaoming2', age: 21 })
For (I = 3; I <30; I ++) db. stu. insert ({_ id: I, 'name': 'xiaoming '+ I, age: 2 + I}) # insert multiple
Db. stu. find ()
{"_ Id": 1, "name": "xiaoming", "age": 20}
{"_ Id": 2, "name": "xiaoming2", "age": 21}
{"_ Id": 3, "name": "xiaoming3", "age": 5}
Db. stu. update ({_ id: 2}, {$ set: {age: 23}) # modify the age whose _ id is 2
Db. stu. find ()
{"_ Id": 1, "name": "xiaoming", "age": 20}
{"_ Id": 2, "name": "xiaoming2", "age": 23}

For (I = 1; I <20; I ++) db. stu. update ({_ id: I}, {$ set: {age: 23}) # batch modify

Db. stu. find ()
{"_ Id": 1, "name": "xiaoming", "age": 23}
{"_ Id": 2, "name": "xiaoming2", "age": 23}
{"_ Id": 3, "name": "xiaoming3", "age": 23}
{"_ Id": 4, "name": "xiaoming4", "age": 23}
{"_ Id": 5, "name": "xiaoming5", "age": 23}

Db. stu. help () # view help
DBCollection help
Db. stu. find (). help ()-show DBCursor help
Db. stu. count ()
Db. stu. copyTo (newColl)-duplicates collection by copying all privileges ents to newColl; no indexes are copied.
Db. stu. convertToCapped (maxBytes)-CILS {convertToCapped: 'stu', size: maxBytes} command
Db. stu. dataSize ()
Db. stu. distinct (key)-e.g. db. stu. distinct ('x ')
Db. stu. drop () drop the collection
Db. stu. dropIndex (index)-e.g. db. stu. dropIndex ("indexName") or db. stu. dropIndex ({"indexKey": 1 })
Db. stu. dropIndexes ()
Db. stu. ensureIndex (keypattern [, options])-options is an object with these possible fields: name, unique, dropDups
Db. stu. reIndex ()
Db. stu. find ([query], [fields])-query is an optional query filter. fields is optional set of fields to return.
E.g. db. stu. find ({x: 77}, {name: 1, x: 1 })
Db. stu. find (...). Count ()
Db. stu. find (...). Limit (n)
Db. stu. find (...). Skip (n)
Db. stu. find (...). Sort (...)
Db. stu. findOne ([query])
Db. stu. findAndModify ({update :... , Remove: bool [, query :{}, sort :{}, 'new': false]})
Db. stu. getDB () get DB object associated with collection
Db. stu. getIndexes ()
Db. stu. group ({key :..., Initial :..., Reduce :... [, Cond:…] })
Db. stu. insert (obj)
Db. stu. mapReduce (mapFunction, reduceFunction ,)
Db. stu. remove (query)
Db. stu. renameCollection (newName,) renames the collection.
Db. stu. runCommand (name,) runs a db command with the given name where the first param is the collection name
Db. stu. save (obj)
Db. stu. stats ()
Db. stu. storageSize ()-Primary des free space allocated to this collection
Db. stu. totalIndexSize ()-size in bytes of all the indexes
Db. stu. totalSize ()-storage allocated for all data and indexes
Db. stu. update (query, object [, upsert_bool, multi_bool])-instead of two flags, you can pass an object with fields: upsert, multi
Db. stu. validate ()-SLOW
Db. stu. getShardVersion ()-only for use with sharding
Db. stu. getShardDistribution ()-prints statistics about data distribution in the cluster
Db. stu. getSplitKeysForChunks ()-calculates split points over all chunks and returns splitter function

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.