Querying all the Databases
Show DBS
Specify to a database/or create
Use database name
Specify storage size
Db.createcollection ("Mycoll", {capped:true,size 10000})
Inserting data
Db. Collection. Insert ({key:values})
Db. Collection. Save ({key:values})
Querying documents
Db. Collection. Find ()
Total query data
Db. Collection. Count ()
MongoDB Limit () method
If you need to read a specified number of data records in MongoDB, you can use the MongoDB limit method, and the limit () method accepts a numeric parameter that specifies the number of record bars read from MongoDB.
Specify the number of bars to read
Db. Set find (). limit (count);
Skips a specified number of rows
Db. Collection. Find (). Limit (count). Skip (count) skip defaults to 0
Sort () method ordering
Parameters are two 1 or-1, ascending or descending
Db. Set find (). Sort ()
Deleting a database
Use database
Dorp. Database ()
Delete Collection
Db.collection.drop ();
Querying database Collections
Db. Collection. Find ()
Update document
Update ()
123456789 |
db.collection. update ( &NBSP;&NBSP;&NBSP; < Query>, &NBSP;&NBSP;&NBSP; < update >, { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP; Code class= "SQL Plain" >upsert: <boolean>, multi: <boolean>, writeconcern: <document> } ) |
Parameter description:
- Query: The search condition for update, similar to where in SQL update query.
- Update: Update object and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
- upsert : Optional, this parameter means that if there is no record of update, insert objnew,true as INSERT, default is False, do not insert.
- multi : Optional, mongodb default is False, only update the first record found, if this parameter is true, will be found on the condition of a number of records update all.
- writeconcern : Optional, throws an exception level.
Example
Db. Collection. Update ({Query condition}, ($set: {Modify content}))
If you want to modify more than one document
Db. Collection. Update ({Query condition}, ($set: {Modify content}), {multi:true})
Eg:db.mylog.update ({"Name": "Zhangsan"}, {$set: {"test": "Test1"}})
Delete a document
Db. Collection. Remove (delete condition)
Delete the first data found
Db. Collection. Remove (delete condition)
Delete all
Db. Collection. Remove ({})
Multi-Key Query
Eg:db. Collection. Find ({key:values,key:values}). Pretty ()//equivalent to the Where condition one and condition two in SQL
Or
Db. Collection. Find (
{
$or [{key:values},
{Key:values}
]
}
). Pretty ()
With AND and OR
Db. Collection. Find ({key:values}, $or [{condition two},{or condition two}]. Pretty ()
MongoDB Index
Db. Collection. Ensureindex ({indexed by what})
Eg:db.numbers.ensureIndex ({num:1})
Explain usage:
Eg:db.numbers.find ({num:{"$gt": 19995}}). explain
Ext.: http://www.cnblogs.com/tsxylhs/p/5657298.html
Basic operation of MongoDB