MongoDB Basics 01

Source: Internet
Author: User
Tags findone

MongoDB Basics

1. Documentation

A document is a basic unit of data in MongoDB, similar to a row in a relational database management system.

A document is an ordered set of key-value pairs. Typically contains one or more key-value pairs.

For example:

{"Greeting": "Hello, world!", "foo", 3}

2. Collection (collection)

A collection is a set of documents, similar to a table in a relational database.

3. Databases (database)

Multiple documents make up a collection, and multiple collections form a database. A MongoDB instance can host multiple databases.

A database of special semantics reserved in MongoDB: admin, local, config.

4. Start MongoDB

Run Mongod and start the database server.

Last Login:fri 22 11:03:36On Ttys001zhangxindemacbook-pro:~zhangxin$ Mongodmongod--help forHelp and startup options2014-08-22t11:51:55.900+0800 [Initandlisten] MongoDB starting:pid=596 port=27017 dbpath=/data/db 64-bit host= zhangxindemacbook-pro.local2014-08-22t11:51:55.901+0800[Initandlisten]2014-08-22t11:51:55.901+0800 [Initandlisten] * * warning:soft rlimits too low. Number of files is a, should be at least 10002014-08-22t11:51:55.901+0800 [Initandlisten] DB version v2.6.42014-08-22t11 : 51:55.901+0800[Initandlisten] git version:3a830be0eb92d772aa855ebb711ac91d658ee9102014-08-22t11:51:55.901+0800 [Initandlisten] Build Info:darwin bs-osx108-4 12.5.0 Darwin Kernel Version 12.5.0:sun Sep 2 9 13:33:47 PDT 2013; root:xnu-2050.48.12~1/release_x86_64 x86_64 boost_lib_version=1_492014-08-22t11:51:55.901+0800[Initandlisten] Allocator:system2014-08-22t11:51:55.901+0800[Initandlisten] options: {}2014-08-22t11:51:55.902+0800 [Initandlisten] Journal dir=/data/db/journal2014-08-22t11:51:55.902+0800[Initandlisten] recover:no journal files present, no recovery needed2014-08-22t11:51:55.996+0800 [Initandlisten] Waiting forConnections on port 27017

Mongod uses the default data directory without parameters,/data/db (C:\data\db in Windows), and server startup fails if the directory does not exist or is not writable.

You can use Mkdir-p/data/db/under Mac or Linux to ensure that the directory you create has write permissions.

Turn off Mongod and use Ctrl + C to close.

5. About MongoDB Shell

When you are sure that Mongod has started, open a new command line to run the MONGO startup shell.

Last Login:fri 11:51:24 on ttys001zhangxindemacbook-pro:~2.6.42014-08-22t11 : 51:55.901+0800

The shell is a fully functional JavaScript interpreter that can run any JavaScript program.

5.1 to see which database is currently pointing, you can use the DB command:

For the convenience of the SQL Shell user, the shell also contains some extensions of non-JavaScript syntax.

Selecting a database using use + dbname (database name) and then using DB to view the DB variable, you find that you have pointed to the dbname database.

Last Login:fri 11:51:24 on ttys001zhangxindemacbook-pro:~2.6.42014-08-22t11 : 51:55.901+08002014-08-22t11:51:55.901+0800 [initandlisten] * * warning:soft rlimits too low. Number of files is should, least 1000> dbtest> Use zhangxindbswitched to db zhangxindb&G T dbzhangxindb

5.2 Basic operations in the shell

5.2.1 Create

The Insert function adds a document to the collection.

Create a local variable called post, which is a JavaScript object that represents our document.

> post = {"title": "My blog post", "Content": "Here ' s My blog post.", "date":new  date ()}    {"title" : "My blog post",    "content": "Here's my blog post." ,    " Date ": Isodate (" 2014-08-22t03:53:31.624z ")}

This object is a valid MongoDB document that is saved to the blog collection using the Insert method.

>"ninserted": 1 })> db.blog.findOne () {    "_id": ObjectId (" 53f6bee6ef70aed220766a98 "),    " title ":" My blog post ",    " content ":" Here's My blog post. " ,    " Date ": Isodate (" 2014-08-22t03:53:31.624z ")}

5.2.2 Read

The Find and FindOne () methods can be used to query the document in the collection. As shown in.

5.2.3 Update

Use Update to modify the blog post. The Update method receives at least two parameters, the first of which is a qualification, and the second is a new document. Like what:

> post.comments = [][ ]>post{"title": "My Blog Post",    "Content": "Here ' s my blog post.",    "Date": Isodate ("2014-08-22t03:53:31.624z"),    "Comments" : [ ]}> db.blog.update ({title: "My Blog Post"},post) Writeresult ({"Nmatched": 1, "nupserted": 0, "nmodified": 1 })>Db.blog.findOne () {"_id": ObjectId ("53f6bee6ef70aed220766a98"),    "title": "My Blog Post",    "Content": "Here ' s my blog post.",    "Date": Isodate ("2014-08-22t03:53:31.624z"),    "Comments" : [ ]}>

4. Delete

Use the Remove method to permanently delete a document from the database. If there are no parameters, all documents within the collection are deleted.

> Db.blog.remove ({title: "My blog Post""nremoved": 1 })> db.blog.findOne ()null 

This article is suitable for 0 basic and want to quickly get started students, write very simple is to use the shortest time to get started, after the introduction, and then gradually deep, carefully dig. Not suitable to have the foundation, or want to slowly chew the classmate, the equivalent is fast food bar. Please advise me of any mistakes.

Reference: the MongoDB authoritative guide

Test environment: Mac 64-bit

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.