Start MongoDB and common operations commands

Source: Internet
Author: User
Tags mongodb server sorts

Nonsql relational database

Collection table

Document Line

  

Manually create a directory that holds MongoDB data files before starting MongoDB, such as E:\mongo_data

Execute command mongod--dbpath=e:\mongo_data

Such trouble

Manually create a file with the suffix named bat, copy the command Mongod--dbpath=e:\mongo_data, and execute it later.

Or take it as a system service and start it on the system service side.

The above operation MongoDB server has been started.

MONGO localhost:27017 connect to the MongoDB server.

MongoDB Common operations

MYDB1 database C1, user collection

Show DBS shows all databases

DB View the current database

Show Collections View all collections in the current database

Show tables view all collections in the current database

Use MYDB1 if the database is present, enter, not exist, create the database

Db.createcollection ("collection name") Displays the Create collection

Db. Collection name. Insert ({name: "Jack", age:20}); Hermits Create collections

Db. Collection name. Drop () Delete collection

Small example:

for (Var i=0;i<10;i++) {

Db.user.insert (Name: "Name" +i,age:i);} 10 Records added

Db.user.count (); Number of output data bars

Db. Collection name. Remove () Delete all data

Db.user.remove ({name: "User0"}) deletes the specified person

Db.user.find () query all

Db.user.find ({name: "User2"}); Query criteria

Db.user.findone (); Query document First

The first box is a query condition. The second box shows those columns that are not displayed,

Db.user.find ({},{name:1}) displays the other properties of the Name property do not display 1 for display 0 for no display

Db.user.find (). Sort ({age:1}) sorts ascending according to age

Db.user.find (). Sort ({age:-1}) sorts descending order according to age

Count statistics Skip limit paging

Skip (i) ignores article I, the Limit page contains a few

First page 4 article skip (0). Limit (4)

Second page 4 article skip (4). Limit (4)

Third page 4 article skip (8). Limit (4)

。。。。。。。。

Db.user.find (). Sort ({age:-1}). Skip (0). Limit (2). Count (0) shows several results, see find () Find out how many bars regardless of pagination effect

Db.user.find (). Sort ({age:-1}). Skip (0). Limit (2). Count (1) shows the paging effect

Db.user.find ({age:{$gt: 5}}) query for older than 5

Db.user.find ({age:{$lt: 5}}) less than 5

$gte $lte $ne can bring in the above command

$all contains relationships in an array

Example

Db.user.insert ({name: "User11", age:22,post:[1,2,3,4]})

Db.user.find ({post:{$all: []}}) post key value, all [all] of the included conditions are detected to find out

In the relational database, the select* from user where ID in (All-in-a-box) is equivalent to the following in Nonsql

$in

Db.user.find ({post:{$in: [1,99]}}) can be found as long as one of the 1 or 99 of the included conditions is present.

$nin and reverse the above

The select* from user where ID name= "Jack" or age=20 in the relational database is equivalent to the following in Nonsql

$or two different conditions

Db.user.find ({$or: [{name: ' User1 '},{age:8}]}) query or condition, two conditions.

$exist: 1 exists

$exist: 0 does not exist

Db.user.find ({post:{$exists: 1}}) there is a post for this key

Db.c1.insert ({name: "Jack", age:20}); To actually form a database file, otherwise there is only a database in memory. C1 is the name of the collection

Db.drop.Database (); Deleting a database

Db.help () See Help information

var x=db.user.find ();

X.next ();

X.next (); A one-way cursor in a record

X.hasnext ();

Db.collection.update (Criteria,objnew,upsert,multi)

Criteria: The object used to set the query criteria

Objnew object for setting updated content

Upsert: If the record already exists, update it, otherwise add a record, the value is 0 or 1 1 already exists update 0 new

Multi: If there are multiple records that match the criteria, whether they are all updated, the value is 0 or 1 1 for all updates

Note: By default, only the first qualifying record is updated

In general, the latter two parameters are 0,1, respectively:

The default is 1,0 if the latter two arguments are not written

Db.collection.update ({},{},0,1);

Db.user.update ({name: "user1"},{name: "ABC"}) will overwrite the entire record and delete the later age

So it should be used at this time $set

$set Update values

Db.user.update ({name: "User3"},{$set {address: "Nanjing"}},0,1)

Db.user.update ({name: "User3"},{$unset: {address:1}},0,1) delete address field

$inc Age plus 1

Db.user.update ({name: "User4"},{$inc: {age:1}})

Start MongoDB and common operations 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.