MongoDB Basics (ii)

Source: Internet
Author: User
Tags findone mongo shell

first, the basic concept1: Document is the basic unit of data in MongoDB, very similar to line 2 in a relational database management system: A collection (collection) can be seen as a dynamic Schema) An instance of table 3:mongodb can have multiple separate databases, each with its own collection 4: Each document has a special key "_id", which is unique within the collection to which the document belongs. 5:mongodb comes with JavaScript shell, can be used to manage MongoDB instance or data operation can look at two graphs, better understanding, this is seen from http://www.runoob.com/here

second, Shell basic OperationFour basic operations: Create, read, update, delete , is the usual said additions and deletions to check 1): CreateThe Insert function adds a document to the collection, such as:
>Show DBS; Admin0. 000GB Local0. 000GB>Use TestDB; Switched to DB TestDB> post={"title":"My Blog Post",    ... "content":"HaHa",    ... "Date": New Date ()} {    "title":"My Blog Post",    "content":"HaHa",    "Date": Isodate ("2017-07-07t03:20:44.698z")    }    >Db.blog.insert (POST) writeresult ({"ninserted":1 })    > Db.blog.Find()    { "_id": ObjectId ("595efe1af713ea7372854b9c"),"title":"My Blog Post","content":"HaHa","Date": Isodate ("2017-07-07t03:20:44.698z") }

2) Read:The Find and FindOne methods are used to query the collection of documents, if you want to see only one document, you can use the FindOne, the above example of the query find query to display up to 20 matching documents
>Db.blog.findOne () {"_id": ObjectId ("595efe1af713ea7372854b9c"),    "title":"My Blog Post",    "content":"HaHa",    "Date": Isodate ("2017-07-07t03:20:44.698z")    } 

3) Update:With update modification, update accepts at least two parameters, the first being a qualification (to match the document to be updated), and the second being a new document. Suppose, on a just basis, to add comment functionality, using array storage
> post.comments=[]    [ ]    > Db.blog.update ({"_id": ObjectId ("595efe1af713ea7372854b9c")},post) Writeresult ({"nmatched":1,"nupserted":0,"nmodified":1 })    >Db.blog.findOne () {"_id": ObjectId ("595efe1af713ea7372854b9c"),    "title":"My Blog Post",    "content":"HaHa",    "Date": Isodate ("2017-07-07t03:20:44.698z"),    "Comments" : [ ]    }
You can see through the query that the comments key has been added 4) Delete:Use the Remove method to permanently delete a document from the database and delete all documents within the collection if no parameter matching is used. It can accept a document as a qualification as a parameter
    > db.blog.remove ({"_id": ObjectId ("595efe1af713ea7372854b9c")  )})    "nremoved"1  })    >  Db.blog.findOne ()    null
As you can see, there are no documents in the collection. Three: Data type null: used to denote null or nonexistent field {"X": null} Boolean : Boolean with two values of true and false {"X": True}Value:The shell defaults to the 64-bit floating-point value {"X": 3.14} or {"X": 3} for integer values, use the Numberint class (representing 4-byte signed integers) {"X": Numberint ("3")} Numberlong Class (representing 8-byte signed integer) {"X": Numberlong ("3")} string:UTF-8 string can be represented as a string type of data {"X": "Foobar"} Date: The date is stored as the number of milliseconds elapsed since the new century, and does not store the time zone {"X": Regular expression: when querying, use regular expressions as qualification {"X":/foobar/i} Array: a data list or dataset can be represented as an array {"X": ["a", "B", "C", "D"]} Inline Documents: documents can be nested in other documents {"X": {"foo": "Bar"}} Object ID: Object ID is a 12-byte ID, which is a unique identifier of the document Techniques for MongoDB Shell:After the connection has entered MONGO, you can use Help to see
>Help Db.help () Help on DB Methods Db.mycoll.help () Help on collection methodsSH. Help () sharding helpers rs.help () replica set helpers Help admin administrative help help connect connecting To a DB help me keys key shortcuts help Misc misc Things to know help Mr MapReduce show DBS Show Database n Ames Show Collections Show CollectionsinchCurrent database show users show UsersinchCurrent database show profiles show most recent system.profile entries with Time>=1ms show logs show the accessible logger names show log [name] prints out the LastSegment of LoginchMemory'Global'is default use<db_name>set Current database Db.foo.Find() List objectsinchcollection Foo Db.foo.Find({a:1}) List objectsinchFoo where a = =1It result of the LastLine evaluated, use to further iterate dbquery.shellbatchsize=x Set default number of items to display on shell exit quit the MONGO shell
View the source code of a function that can be used directly, such as: db.test.update will show you the update function.

MongoDB Basics (ii)

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.