Basic operation examples of common MongoDB data tables

Source: Internet
Author: User
Tags findone mongodb

View all data tables

> Use ChatRoom
Switched to db ChatRoom
> Show collections
Account
Chat
System. indexes
System. users
 

Create a data table

> Db. createCollection ("Account ")
{"OK": 1}
 

> Db. createCollection ("Test", {capped: true, size: 10000}) {"OK": 1}
{"OK": 1}
-- Description

Capped: true, indicating that the structure of the set cannot be modified;

Size: At the beginning of the table creation, a certain amount of space is specified. The next insert operation will constantly APPEND the data in the pre-allocated space File. If the size of the space has exceeded, then, the system will return to the file header to overwrite the original data and continue the insertion. This structure ensures the efficiency of insertion and query. It does not allow the deletion of a single record, and there are restrictions on the update: the size of the original record cannot exceed. This type of table is highly efficient. It is suitable for some scenarios where data is temporarily stored, such as the session information of users logged on to the website, and the monitoring logs of some programs, all data that can be overwritten after a certain period of time.

 
Modify data table name

> Db. Account. renameCollection ("Account1 ")
{"OK": 1}
 

Help topic of data table help


> Db. Account. help ()
DBCollection help
Db. Account. find (). help ()-show DBCursor help
Db. Account. count ()
Db. Account. dataSize ()
Db. Account. distinct (key)-eg. db. Account. distinct ('x ')
Db. Account. drop () drop the collection
Db. Account. dropIndex (name)
Db. Account. dropIndexes ()
Db. Account. ensureIndex (keypattern [, options])-options is an object with these possible fields: name, unique, dropDups
Db. Account. reIndex ()
Db. Account. find ([query], [fields])-query is an optional query filter. fields is optional set of fields to return.
E.g. db. Account. find ({x: 77}, {name: 1, x: 1 })
Db. Account. find (...). count ()
Db. Account. find (...). limit (n)
Db. Account. find (...). skip (n)
Db. Account. find (...). sort (...)
Db. Account. findOne ([query])
Db. Account. findAndModify ({update:..., remove: bool [, query :{}, sort :{}, 'New': false]})
Db. Account. getDB () get DB object associated with collection
Db. Account. getIndexes ()
Db. Account. group ({key:..., initial:..., reduce:... [, cond:...]})
Db. Account. mapReduce (mapFunction, performancefunction, <optional params>)
Db. Account. remove (query)
Db. Account. renameCollection (newName, <dropTarget>) renames the collection.
Db. Account. runCommand (name, <options>) runs a db command with the given name where the first param is the collection name
Db. Account. save (obj)
Db. Account. stats ()
Db. Account. storageSize ()-Primary des free space allocated to this collection
Db. Account. totalIndexSize ()-size in bytes of all the indexes
Db. Account. totalSize ()-storage allocated for all data and indexes
Db. Account. update (query, object [, upsert_bool, multi_bool])
Db. Account. validate ()-SLOW
Db. Account. getShardVersion ()-only for use with sharding
 

View all table records

> Db. Account. find ()
{"_ Id": ObjectId ("4df08553188e444d001a763a"), "AccountID": 1, "UserName": "libing", "Password": "1", "Age ": 26, "Email": "libing@126.com", "RegisterDate": "16:31:25 "}
{"_ Id": ObjectId ("4df08586188e444d001a763b"), "AccountID": 2, "UserName": "lb", "Password": "1", "Age ": 25, "Email": "libing@163.com", "RegisterDate": "16: 36: 95 "}
 
-- SELECT * FROM Account
 
Note:
 

By default, 20 records are displayed on each page. If no record is displayed, you can use the it iteration command to query data on the next page.
You can use DBQuery. shellBatchSize to set the size of data displayed on each page. For example, DBQuery. shellBatchSize = 5, so that five records are displayed on each page.

> Db. Test. find ()
{"_ Id": ObjectId ("4df6d55407444568af61cfea"), "TestID": 1}
{"_ Id": ObjectId ("4df6d55907444568af61cfeb"), "TestID": 2}
{"_ Id": ObjectId ("4df6d55b07444568af61cfec"), "TestID": 3}
{"_ Id": ObjectId ("4df6d55e07444568af61cfed"), "TestID": 4}
{"_ Id": ObjectId ("4df6d56207444568af61c133"), "TestID": 5}
{"_ Id": ObjectId ("4df6d56507444568af61cfef"), "TestID": 6}
{"_ Id": ObjectId ("4df6d56807444568af61cff0"), "TestID": 7}
{"_ Id": ObjectId ("4df6d56b07444568af61cff1"), "TestID": 8}
{"_ Id": ObjectId ("4df6d56e07444568af61cff2"), "TestID": 9}
{"_ Id": ObjectId ("4df6d57a07444568af61cff3"), "TestID": 10}
{"_ Id": ObjectId ("4df6d57d07444568af61cff4"), "TestID": 11}
{"_ Id": ObjectId ("4df6d58007444568af61cff5"), "TestID": 12}
{"_ Id": ObjectId ("4df6d58307444568af61cff6"), "TestID": 13}
{"_ Id": ObjectId ("4df6d58e07444568af61cff7"), "TestID": 14}
{"_ Id": ObjectId ("4df6d59207444568af61cff8"), "TestID": 15}
{"_ Id": ObjectId ("4df6d59607444568af61cff9"), "TestID": 16}
{"_ Id": ObjectId ("4df6d59c07444568af61cffa"), "TestID": 17}
{"_ Id": ObjectId ("4df6d5a307444568af61cffb"), "TestID": 18}
{"_ Id": ObjectId ("4df6d5a607444568af61cffc"), "TestID": 19}
> DBQuery. shellBatchSize
20
> DBQuery. shellBatchSize = 5
5
> Db. Test. find ()
{"_ Id": ObjectId ("4df6d55407444568af61cfea"), "TestID": 1}
{"_ Id": ObjectId ("4df6d55907444568af61cfeb"), "TestID": 2}
{"_ Id": ObjectId ("4df6d55b07444568af61cfec"), "TestID": 3}
{"_ Id": ObjectId ("4df6d55e07444568af61cfed"), "TestID": 4}
{"_ Id": ObjectId ("4df6d56207444568af61c133"), "TestID": 5}
Has more
> It
{"_ Id": ObjectId ("4df6d56507444568af61cfef"), "TestID": 6}
{"_ Id": ObjectId ("4df6d56807444568af61cff0"), "TestID": 7}
{"_ Id": ObjectId ("4df6d56b07444568af61cff1"), "TestID": 8}
{"_ Id": ObjectId ("4df6d56e07444568af61cff2"), "TestID": 9}
{"_ Id": ObjectId ("4df6d57a07444568af61cff3"), "TestID": 10}
Has more
> It
{"_ Id": ObjectId ("4df6d57d07444568af61cff4"), "TestID": 11}
{"_ Id": ObjectId ("4df6d58007444568af61cff5"), "TestID": 12}
{"_ Id": ObjectId ("4df6d58307444568af61cff6"), "TestID": 13}
{"_ Id": ObjectId ("4df6d58e07444568af61cff7"), "TestID": 14}
{"_ Id": ObjectId ("4df6d59207444568af61cff8"), "TestID": 15}
Has more
> It
{"_ Id": ObjectId ("4df6d59607444568af61cff9"), "TestID": 16}
{"_ Id": ObjectId ("4df6d59c07444568af61cffa"), "TestID": 17}
{"_ Id": ObjectId ("4df6d5a307444568af61cffb"), "TestID": 18}
{"_ Id": ObjectId ("4df6d5a607444568af61cffc"), "TestID": 19}
> It
No cursor
 

Query a record

> Db. Account. findOne ()
{
"_ Id": ObjectId ("4ded95c3b7780a774a099b7c "),
"UserName": "libing ",
"Password": "1 ",
"Email": "libing@126.cn ",
"RegisterDate": "11:06:25"
}
-- Select top 1 * FROM Account
 

Query different records of fields in clustering

> Db. Account. distinct ("UserName ")
-- Select distinct ("UserName") FROM Account
 

Query records in which the UserName in the cluster contains the "keyword" keyword.

Db. Account. find ({"UserName":/keyword /})
-- SELECT * FROM Account WHERE UserName LIKE '% keyword %'
 

Query records whose usernames start with "keyword" in a cluster

> Db. Account. find ({"UserName":/^ keyword /})
-- SELECT * FROM Account WHERE UserName LIKE 'keyword %'
 

Query the records whose usernames end with "keyword" in the cluster.

> Db. Account. find ({"UserName":/keyword $ /})
-- SELECT * FROM Account WHERE UserName LIKE '% keyword'
 

Columns specified in the query clustering

> Db. Account. find ({}, {"UserName": 1, "Email": 1}) -- 1: true
-- SELECT UserName, Email FROM Account

 
Exclude specified columns from query clustering

> Db. Account. find ({}, {"UserName": 0}) -- 0: false
 
Specify a column in the cluster query, and the Age is greater than 20.

> Db. Account. find ({"Age": {"$ gt": 20 },{ "UserName": 1, "Email": 1 })
-- SELECT UserName, Email FROM Account WHERE Age> 20
 

Sort fields in clustering

> Db. Account. find (). sort ({"UserName": 1}) -- Ascending
> Db. Account. find (). sort ({"UserName":-1}) -- descending
-- SELECT * FROM Account order by UserName ASC

-- SELECT * FROM Account order by UserName DESC
 

Count the number of records in the aggregation

> Db. Account. find (). count ()
-- Select count (*) FROM Account
 

Count the number of qualified records in the aggregation

> Db. Account. find ({"Age": {"$ gt": 20}). count ()
-- Select count (*) FROM Account WHERE Age> 20
 

Count the number of records that meet the condition in the clustering field

> Db. Account. find ({"UserName": {"$ exists": true}). count ()
-- Select count (UserName) FROM Account
 

Query the first five records in a cluster


> Db. Account. find (). limit (5)
-- Select top 5 * FROM Account

Query the records after 10th in a cluster

> Db. Account. find (). skip (10)
-- SELECT * FROM Account WHERE AccountID not in (select top 10 AccountID FROM Account)
 
Query the next five records of the first 10th records in the aggregation

> Db. Account. find (). skip (10). limit (5)
-- Select top 5 * FROM Account WHERE AccountID not in (select top 10 AccountID FROM Account)


Or query

> Db. account. find ({"$ or": [{"UserName":/keyword/}, {"Email":/keyword/}]}, {"UserName": true, "Email": true })
-- SELECT UserName, Email FROM Account WHERE UserName LIKE '% keyword %' OR Email LIKE '% keyword %'
 

Add new record

> Db. account. insert ({AccountID: 2, UserName: "lb", Password: "1", Age: 25, Email: "libing@163.com", RegisterDate: "16: 36: 95 "})

Modify record

> Db. Account. update ({"AccountID": 1 },{ "$ set": {"Age": 27, "Email": "libingql@163.com "}})
> Db. Account. find ({"AccountID": 1 })
{"AccountID": 1, "Age": 27, "Email": "libingql@163.com", "Password": "1", "RegisterDate": "16:31:25 ", "UserName": "libing", "_ id": ObjectId ("4df08553188e444d001a763a ")}
 
> Db. Account. update ({"AccountID": 1 },{ "$ inc": {"Age": 1 }})
> Db. Account. find ({"AccountID": 1 })
{"AccountID": 1, "Age": 28, "Email": "libingql@163.com", "Password": "1", "RegisterDate": "16:31:25 ", "UserName": "libing", "_ id": ObjectId ("4df08553188e444d001a763a ")}
 

Delete record

> Db. Account. remove ({"AccountID": 1}) -- delete from Account WHERE AccountID = 1

> Db. Account. remove ({"UserName": "libing"}) -- delete from Account WHERE UserName = 'libbing'
> Db. Account. remove ({"Age": {$ lt: 20}) -- delete from Account WHERE Age <20
> Db. Account. remove ({"Age": {$ lte: 20}) -- delete from Account WHERE Age <= 20
> Db. Account. remove ({"Age": {$ gt: 20}) -- delete from Account WHERE Age> 20
> Db. Account. remove ({"Age": {$ gte: 20}) -- delete from Account WHERE Age> = 20
> Db. Account. remove ({"Age": {$ ne: 20}) -- delete from Account WHERE Age! = 20
 

> Db. Account. remove () -- delete all
> Db. Account. remove ({}) -- delete all

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.