MongoDB Common Commands

Source: Internet
Author: User
Tags findone sorts

Go from http://www.blogjava.net/waterjava/archive/2011/03/23/346819.html command line
--help Display command line arguments
--nodb Start without a database connection, and you can later use new Mongo () or connect () to establish a connection
--shell After you run a. js file from the command line, stay in the shell, not the end
Special commands

Non-JavaScript auxiliary instructions:

Help Show Help
Db.help () Show DB Method Help
Db. mycoll . Help () Show Clustered Methods Help
Show DBS List of all databases on the print server
Use dbname Set the DB variable to indicate the use of the dbname database on the server
Show collections Print all aggregates for the current database
Show Users Print the current database user
Show profile Print profiling operations with a recent time-consuming greater than 1ms
Basic Shell JavaScript operations
Db The variables that point to the current database objects and connections are already defined in your instance.
Db.auth (User,pass) Database authentication (if Safe mode is running)
coll = db. Collection Access to specific collection in the database
cursor = Coll.find () Finds all objects in the cluster. Refer to [query].
Coll.remove (objpattern ) Deletes a matching object from the aggregation.
Objpattern is an object that specifies a matching field, for example:coll.remove ({name: "Joe"});
Coll.save (object ) Saves the object in the aggregation and updates it if it already exists.
If the object has a Presave method, the method is called before it is saved to the database (before inserting and updating).
Coll.insert (object) Inserts an object into the cluster. Does not check whether the object already exists in the aggregation (that is, not upsert)
Coll.update (...) Updates the object in the aggregation. Update () has many parameters, please review the update documentation.
Coll.ensureindex ({ name : 1}) Index the name . Do nothing if the index exists.
Coll.drop () Delete Coll aggregation
DB.GETSISTERDB (name) Returns another database for the current connection. It allows querying across databases, for example:db.getsisterdb (' production '). Getcollectionnames ()
Inquire
Coll.find () Querying all Documents
It Loop the last Find () call returned by the cursor
Coll.find ( criteria ); Queries the object that matches criteria in the aggregation. For example:coll.find ({name: "Joe"});
Coll.findone ( criteria); Queries and returns an object. Returns null if it is not found. If you only need to return an object, this method is more efficient than the find () as limit (1) . If the element type is a string, number, or time, you can also use regular expressions:coll.find ({name:/joe/i});
Coll.find (criteria, fields); Queries the specific domain in the object. For example:coll.find ({}, {name:true});
Coll.find (). Sort ({field : 1[, field : 1]}); Sorts the returned results (field ASC). Use 1 to indicate DESC.
Coll.find ( criteria ). Sort ({ field : 1}) Finds the object that matches the criteria and sorts the field .
Coll.find (...). Limit (n ) Limit the result to return n rows. If you only need a few rows of data, this is recommended for optimal performance.
Coll.find (...). Skip (n) Skips the results of n rows.
Coll.count () Returns the total number of objects in the aggregation.
Coll.find (...). Count () Returns the total number of objects matching the query. Note that the return ignores limit and skip. For example, there are 100 rows of records that match the query, but the limit is ten, andcount () will still return 100. This is faster than your own cycle, but it still takes some time.

For more information, please refer to [enquiry].

Error checking
[{{Db.getlasterror ()}}] Returns the error of the last operation
Db.getpreverror () Return error of previous operation
Db.reseterror () Clear Error logging
Manage commands
Db.clonedatabase (Fromhost) Copies the current data database from another specified host. The fromhost must be in Noauth mode.
Db.copydatabase (Fromdb, Todb, Fromhost) Copy the Fromhost Fromdb database to the TODB database of the current server. The fromhost must be in Noauth mode.
Db.repairdatabase () Repairs the current database. This operation can be very slow if the database is large.
Db.adduser (USER,PWD) Adds a user to the current database.
Db.getcollectionnames () Get a list of all the aggregates.
Db.dropdatabase () Deletes the current database.
Open Additional connections
db = Connect (" Open a new database connection. A shell may have multiple connections, but the shell's automatic GetLastError is used only for ' db ' variables.
conn = new Mongo ("hostname") Open a new server connection. You can then use getdb () to select a database.
db = Conn.getdb ("dbname") Select a specific database for a connection.
Other
Object.bsonsize (Db.foo.findOne ()) Prints the Bson size of a Database object (MONGO version 1.3 and above)
Db.foo.findOne (). Bsonsize ()

Prints the Bson size of a database object (before MONGO version 1.3)

MongoDB Common Commands

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.