mongodb--Summary

Source: Internet
Author: User
Tags auth mongodb
Switch to a database using use, db. Table name. Command name ({parameter}) to perform related actions. At this point the DB refers to the default switch to the DB
Lookup is usually the entire document, you need to get to a field in the document, {field name: 1}
Some of the more strange wording find ({},{},number1,number2), Number1 is Limit,number2 is skip, will be in booster error ...
The first default should be query criteria, and the second default should be the result of the query.
Displays the Create fixed collection createcollection ("document name", {capped:true,size:20480,max:100}), specifies the maximum size, the maximum number of documents inserted, and ensures that the insertion order is consistent, ensuring that the changes are not changed in order. If you want to get the first n values, use the limit
If you get the latter 10 values, you can use $natural:-1 to reverse the natural order
The Validate () function can check the size of the collection.
The Count function essence and skip () are conflicting, and the key to the solution is count (true)
The Distinct () function can get a property directly and query all possible results, but the number of returns is limited.
Group () function, there are three parameters, Key,initial,reduce function, the number of groups can not exceed 10000, otherwise it will be an error.
$In, $Nin, $or, $and
$slice: []-----Array operations, specify the number of elements to return an array, or use it for document Db.zzy_test.find ({' Name ': ' Philosophy '},{' Pic.name ': {$slice: 2}} ') or all the information returned, cannot be used with-1.
$push: Inserts the element to the end of the array, which needs to be combined with the update instruction Db.zzy_test.update ({"Name": "Philosophy"},{$push: {' Pic.thumb ': 2}})
$mod: The remainder operation, specifying how many Db.zzy.find ({"Age": {$mod: [2,0]}})
$size: Specifies the size of the array, the exact matching value. Db.zzy_test.find ({"Pic.info": {$size: 2}})
$exists: Does the field exist, but cannot use the index, scan the entire table
JavaScript code,
Find ("This.age < 3")
var f
f = function () {return This.age < 3}
Db.zzy.find (f)
$push: Add content to an array for an array, but only one at a time, [] the contents of the brackets are stored as a child array. Therefore, it should be noted that if you want to push multiple elements, you need to precede [] with $each, $slice can be used as a parameter, limit the number of elements in the array, need to use with $each
$addToSet: The added array cannot be repeated $push: The added array can be repeated
Db.zzy_test.update ({' Name ': ' Philosophy '},{$addToSet: {' pic.thumb ': [' WOCA2 ']}})
$unset db.zzy_test.update ({' Name ': ' Philosophy '},{$unset: {' pic.thumb ': ' Woyun2 '}}) deletes the corresponding key value, regardless of whether the match succeeds, the deletion is thumb this array ...
$inc can only add and subtract numbers db.zzy_test.update ({' Name ': ' Philosophy '},{$inc: {' LoadState ': 4}}})
The $pop command does not delete the array, it only deletes the elements of the array, which is not the same as unset. And unset will erase everything that matches.
Db.zzy.update ({name: "user_1"},{$pop: {"names": 1}}) You can specify whether the pop-up is the first or last
Db.zzy.update ({name: "user_1"},{$push: {' names ': {$each: [' Zhaoliu ', ' Wangwu ']}}})
Db.zzy.update ({name: "user_1"},{$pullAll: {' names ': [' Zhaoliu ', ' Wangwu ']}})
$pull itself can remove duplicate array elements, but you cannot use [] to remove multiple elements.
$pullAll can remove multiple elements
Db. $cmd. FindOne ({getlasterror:1}) view last error message
Db.zzy.remove ({"Name": "user_1"}) Deleting a document
Db.zzy.renameCollection ("NewName") renamed
Db.zzy.remove ({}) Delete all documents
Db.zzy.drop () Delete collection
Db.dropdatabase () Delete database, note which database is currently in use


Db.zzy.ensureIndex ({"Age": 1})
Db.zzy.find (). Min ({age:1}). Max ({age:3})
The use of Min Max must be indexed, otherwise it will be an error


MapReduce function. Composed of three parts, Map,reduce, finalize--used to handle the results of Reduce returns
MapReduce----Flexible aggregation functions----efficient


Mongodump: Backup-D databasename-c collectionname
The default backup directory structure is./dump/databasename/collectionname.bson
The. Bson file is a replica. json file is index and definition
Mongorestore: Restore--drop Delete existing data and restore, without this command will have an additional effect (note the backup directory and reply to the directory to switch to)
Mongoimport can be loaded in a file format that has a CSV TSV for the first two, you can specify the first row as a list of names. Json
Mongoimport-d-C--type csv--headerline < xx.csv where a list of categories and names is specified
Mongoexport
Mongoexport-d-c-q{}-f-id,title,message,author--csv > Blogpost.csv where-Q is the query condition and-F is the field
Mongod: Need to start the MongoDB service first
Mongo: Start the MongoDB database


About--auth instruction, open user authentication
Switch to a database, using the
Db.createuser ({User: "admin", pwd: "Pass", roles:["ReadWrite", "Dbadmin"]})
Turn off the Mongod and reopen Mongod--auth
Db.removeuser ("admin")
After landing the database, after switching to a database, using Db.auth ("User", "pass") can
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.