MongoDB Basic Use

Source: Internet
Author: User
Tags findone modifier mongodb reserved

MongoDB self-javascript shell MongoDB Shell command db view which database is currently being directed to create Db.col.insert (POST) read Db.col.find ()
Db.col.findOne () Update post.comments=[] Db.col.update ({title: "My Blog Post"},post)
The update function has two parameters at least, the first parameter matches the document to be updated, and the second parameter is the new document. Delete Db.col.remove ({title: "My Blog Post"})
Db.col.remove ()//Delete all documents in the collection
Db.col.drop ()//delete collection data type

JSON has only null, Boolean, numeric, string, array, and object data types.
MongoDB Data type:
-NULL
-Boolean
-Numeric default 64-bit floating-point number; Numberint ("3") represents a 4-byte signed integer; Numberlong ("3") represents a 8-byte signed integer;
-String Support UTF-8 string
-date, does not distinguish time zone. The number of milliseconds since the epoch; the object to be returned with the new date () instead of the date (), rather than the date;
-Regular Expressions
-Array
-Arrays can contain different types of elements

-array can even be nested arrays

-MongoDB can understand the structure of the array, and deep inside its content to operate;
Inline document

Object ID, 12-byte ID, which is the unique identity of the document. ObjectId ()

The ID of the document in each collection is unique, and different collection document IDs can be duplicated

The first 4 bytes are timestamps, starting with the standard era, and the unit is seconds;

4-6 bytes is the hash value of the machine host name (hash);

7-8 bytes is the process identifier (PID) that produces the objectid process, which distinguishes multithreading on the same machine;

9-11 Bytes is an automatically incremented counter that guarantees that the same objectid is different from the same second, and that 2,563 Objectid are allowed in one second.

binary data; UTF-8 strings to save code in this form; You can insert arbitrary JavaScript code in queries and documents; use a database connected to a specified machine: MONGO machine Name: Port/Database name startup shell does not connect to any database: Mongo-nodb connect to the specified database again:

conn = new Mongo ("some-host:30000")
db = Conn.getdb ("MyDB")
> Help get assistance just enter the function name to view the implementation code MONGO script1.js script2.js script3.js MONGO--quiet server-1:30000/foo scirpt1.js Script 2.js
Specify host, database, execute Script Shell helper functions cannot be used in files, such as show DBS corresponds to Db.getmongo (). Getdbs () Show collections corresponds to Db.getcollectionnames () use Foo corresponds to Db.getsisterdb ("foo") Load ("defineconnectionto.js") Load execution > run ("ls", "L", "/usr/local") to execute the command-line program Collection naming if you include reserved words db.getcollection ("version") > Object.bsonsize (DOC) view the document size; Required document size must be less than 16MB document replacement
> var Jone = db.col.findOne ({name: "Jone"})
> jone.releationships = {friends:jone.friends,enemies: Jone.enemies}
{"Friends": +, "enemies": 2}
> Jone.usename=jone.name
jone
> Delete Jone.friends
true
> Delete jone.enemies
true
> Delete jone.name
true
> Db.col.update ({name: "Jone"},jone)
Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1})
> Db.col . Find ()
{"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": *, "Enemies": 2}, "Usenam E ":" Jone "}
An atomic update modifier that updates certain fields in the specified document
$inc modifier is used to increase the value of a key, and if it does not exist, create this key > db.col.update ({usename: "Jone"},{"$inc": {friends:1}}) Writeresult ({"nmatched":  1, "nupserted": 0, "nmodified": 1}) > Db.col.find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships" : {"Friends": "Enemies": 2}, "Usename": "Jone", "Friends": 1} > Db.col.update ({usename:jone},{"$set": {Frien Ds:2}}) Writeresult ({"nmatched": 0, "nupserted": 0, "nmodified": 0}) > Db.col.find () {"_id": ObjectId ("5811c15b3 6c14a6dd31ec9f5 ")," Releationships ": {" Friends ":" Enemies ": 2}," Usename ":" Jone "," Friends ": 1} > Db.col.u Pdate ({usename: "Jone"},{"$set": {friends:2}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.col . Find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": +, "enemies": 2}, "Usename": "Jone", "Friends": 2}//unst Delete this key > db.col.update ({usename: "Jone"},{"$unset": {friends:1}}) Writeresult ({" Nmatched ": 1," nupserted ": 0," NmodifiEd ": 1}" > Db.col.find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": "Enemie" S ": 2}," Usename ":" Jone "}
$push adds an element to the end of the array, and if the array does not exist, a new array is created
> Db.col.find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": *, "Enemies": 2}, " Usename ": Jone"} > Db.col.update ({usename: "Jone"},{"$push": {comments:{name: "Joe", Email: "Joe@example.com", Content: "Nice"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}) > Db.col.find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": "Enemies": 2}, "Usename": "Jone", "comments": [{] Name ": Joe", "email": "joe@example.com", "content": "Nice"}]} > Db.col.update ({usename: "Jone"},{"$push": {comment S:{name: "Bob", Email: "Bob@example.com", Content: "Good"}}) Writeresult ({"nmatched": 1, "nupserted": 0, "nmodified": 1}  ) > Db.col.find () {"_id": ObjectId ("5811c15b36c14a6dd31ec9f5"), "Releationships": {"Friends": +, "enemies": 2},  "Usename": "Jone", "comments": [{"Name": "Joe", "email": "joe@example.com", "content": "Nice"}, {"Name": "Bob", "Email": "bob@example.com", "ConteNT ":" Good "}]} 
The "Each" operator and the "each" and "push" can be inserted at a time by inserting multiple elements "$slice" to limit the length of the array "$sort" to sort the "$ne" condition test, such as inserting the data into an array if there is no content inserted "$ Addtoset "is also a test of heavy {" $pop ": {" key ": 1}} Deletes an element from the end of the array
{' $pop ': {' key ':-1}} deletes an element from the header of the array
{' $pull ': {tem: ' Flower '}} Remove all "Flower" in the array
The first identifier "$" db.col.update ({"Commments.author: Jone"},{"$set": {"comments.$.authors:" Jim "}}) $ comments.0 array can be accessed by subscript Inc. is relatively fast because it is modified in place without changing the structure of the document; The array modifier may want to change the document's size slowly, and the fill factor (Paddingfactor) is the growth space reserved for each new document MONGODB. With a document growing, the fill factor becomes larger, and if subsequent updates result in more document movement, the fill factor continues to grow, and if no more document moves, the fill factor will slow down to 1. The Upsert update is an update when an object that meets the update criteria is found, a new document is created by the update criteria when it is not located, and the field is create and assigned when the document is created, and the value is no longer changed when all the update operations are made later; The fourth parameter of the update function is true to indicate that when there are multiple qualifying documents, the GetLastError command can return information about the last action; the value of key n indicates the number of documents updated Db.runcommand ({ GETLASTERROR:1}) Findandmodify returns the value of the document before the update condition is met;
PS = Db.runcommand ({"findandmodify": "Processes",
    query:{status: "READY"},
    sort:{priority:-1},
    update: {"$set": {stats: "RUNNING"}}}). Value
Write security (write concern) is a client-side setting that controls the security of writing, divided into two types: answer-write (the database responds, tells you whether to write successfully), and non-answering writes (not knowing if the write succeeded). The default is an answer; The Shell's support for non-answering is different from that of the client: The shell performs a non-answer check to see if the last operation was successful, even if the previous series of invalid operations, the last operation is valid, there will be no error; 2012 ago, the default write safe way is How to differentiate: The new Mongoclient class used, the Mondo or connection or others used for the old connection object;
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.