Configuration and use of MongoDB

Source: Internet
Author: User
Tags syslog

Two ways to run MongoDB

Check if there are Mongodb:which Mongod

Create DATABASE Storage directory: Mkdir-p/data/db

Check the disk directory for space (typically larger than 4G): DF-LH

Start: A direct start: Mongod--dbpath=/data/db--port=27017

B daemon Start: Mongod--dbpath=/data/db--port=27017--fork--syslog//syslog for system logs It's best to use MongoDB's own logs

Best written: Mongod--dbpath=/data/db--port=27017--fork--log=/var/log/mongod.log

Basic use of MongoDB

On the premise that the daemon is turned on

Running MongoDB

      1. MONGO 127.0.0.1:27017
      2. Mongo

Display database: Show DBS

Create DATABASE: Use MyDB//Note The database is only displayed when the number of data bars is >=1

Insert a piece of data in the Users collection: Db.users.insert ({"Name": "Xiao Wang", "Sex": "Male"})

See all collections: Show Collections

Querying all data from the Users collection: Db.users.find () queries the users collection for data that specifies conditions: Db.user.find ({"Name": "Xiao Wang"})

To view the number of users collection data: Db.users.find (). Count () View the number of data bars for the specified condition Db.user.find ({"Name": "Xiao Wang"}). Count ()

Modify data for the Users collection: db.users.update (condition, modify value, configuration parameter) db.users.update ({"Name:" Xiao Wang "},{$set: {" Age ":" ""}},{multi:true})// Multi refers to whether to modify all the data that meets the criteria, by default modify the first bar

Modify data for the Users collection: Db.users.save ({"_id": ObjectId ("5ae81be4ce517088bda1f195"), "name": "Small Week"})//Note _id is a required field

Delete data from the Users collection: Db.users.remove ({"Name": "Xiao Wang"},true)//true indicates whether to delete a single row, by default deleting all data that meets the criteria

Remove removes the entire document and cannot delete the index

To delete all, use drop

Db.users.drop ()

Summary of the difference between save and update: Save must modify a field must be added to all other fields, and update can specify the field modification, with $set: specified can be

  

Configuration and use of MongoDB

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.