MongoDB Learning Note 06

Source: Internet
Author: User
Tags findone mongodb server unique id

Delete a collection in the shell, execute db.test.drop () or Db.runcommand ({"Drop": "Test"}), and the commands in MongoDB are actually implemented as a special type of query that executes against the $cmd collection, RunCommand simply accepts the command document and executes the equivalent query, so the drop invocation is actually like this:

Db. $cmd. FindOne ({"Drop": "Test"})

Db.listcommands () List all the commands

MongoDB supports fixed collections, which are created in advance and fixed in size, the fixed set is much like a ring queue, and if there is not enough space, the earliest documents are deleted, freeing up space for new documents. Fixed collections do not have an index by default, even on "_id".

Create a pinned collection you can use CreateCollection to create

Db.createcollection ("my_collection", {capped:true, size:+})

You can convert a normal collection to a fixed set

Db.runcommand ({"converttocapped":"my_collection", Size: +})

Fixed collections have a special sort of natural ordering, which is the order of the documents on disk, because the fixed set of documents is always stored in the order in which they are inserted, and the natural order is the same. It can also be queried in the order of Backwash insertions, as follows:

Db.my_collection.find (). Sort ({"$natural":-1})

Using {"$natural": 1} indicates the same as the default order, and non-fixed collections do not guarantee that documents are stored in a particular order, so the natural order does not make much sense.

A trailing cursor is a special persistent cursor that is not destroyed after it has no results, and once a new document is added to the collection it is retrieved and output, and the trailing cursor can only be used on a fixed set. "The MongoDB shell does not support trailing cursors"

Gridfs is a mechanism for storing a binary file in MongoDB

PS D:\install\MongoDB\Server\3.0>. \bin\mongofiles.exe Search Foo --Geneva-07T13: to:03.326+0800Connected To:localhostps D:\install\MongoDB\Server\3.0>. \bin\mongofiles.exe put Foo.txt --Geneva-07T13: to:17.425+0800connected to:localhostadded File:foo.txtPS D:\install\MongoDB\Server\3.0>. \bin\mongofiles.exeGetfoo.txt --Geneva-07T13: to:24.541+0800Connected to:localhostfinished writing To:foo.txtPS D:\install\MongoDB\Server\3.0>. \bin\mongofiles.exe Delete foo.txt --Geneva-07T13: to:33.263+0800Connected to:localhostsuccessfully deleted all instances of'foo.txt'  fromGridfs

Gridfs is a lightweight storage specification built on a common mongodb document, and MongoDB server has no special care for GRIDFS requests, and all related work is done by the client driver or tool. One of the basic ideas of GRIDFS is that you can divide large files into small chunks, each stored as a single document, so that you can store large files, in addition to the blocks that store the file itself, and a separate document that stores the metadata of the block's information and files.

The GRIDFS block has a separate collection, and by default the block will use the Fs.chunks collection

The structure inside the fs.chunks is as follows:

{

"_id", the unique ID of the block

"N", the number of the block that is the ordinal number of the block in the original file

"Data" containing binary data that makes up the file block

"files_id" contains the "_id" of the file document for this block meta-data

}

The metadata for the file is placed in another collection, the default is Fs.files, where each document on the side represents a file in Gridfs, and the custom metadata associated with the file can exist, in addition to the user-defined key, the GRIDFS specification defines some keys.

_id file unique ID, stored as the value of the "files_id" key in the Block

The total number of bytes in the length file

ChunkSize the size of each block, bytes, default 256K, can be adjusted if necessary

Timestamp of the Uploaddata file being stored in Gridfs

MD5 MD5 checksum of the contents of the file, generated by the server side

JavaScript scripts can be executed on the server side via the Db.eval function

> Db.eval ("return 1; " )1> Db.eval ("function () {return 1;} " )1> Db.eval ("function (A, b) {return a+b;} ",1,2)3

Each MongoDB database has a special collection system.js, which is used to store JavaScript variables that can be invoked in any MongoDB JavaScript context.

>Db.system.js.find ()> Db.system.js.insert ({"_id":"x","value":1}) Writeresult ({"ninserted":1 })> Db.system.js.insert ({"_id":"y","value":2}) Writeresult ({"ninserted":1 })> Db.system.js.insert ({"_id":"Z","value":3}) Writeresult ({"ninserted":1 })> Db.eval ("return x+y+z;")6>Db.system.js.find () {"_id":"x","value":1 }{ "_id":"y","value":2 }{ "_id":"Z","value":3}

The disadvantage of using the stored JavaScript is that the code will be detached from the regular source control, which will disrupt the JavaScript sent by the client, the best way to use the storage JavaScript is that there are many places in the program to use a JavaScript function, Put such a function in a central location, and if there is an update, you do not have to modify it every place.

Dbref (Database Reference) is like a URL that uniquely identifies a reference to a document.

Dbref is an inline document, but DBREF has some required keys "$ref", "$id" optional Key "$db"

The order of the keys in the dbref cannot be changed, the first one must be "$ref", followed by "$id" and then (optional) "$db"

{"$ref": Collection, "$id": Id_value, "$db":d atabase}

Collection: Point to a collection

Id_value: A unique document determined by "_id" in the collection

Database: Databases

>Db.user.find ()>Db.users.find () {"_id":"Mike","Display_name":"Mike D" }{ "_id":"Kristina","Display_name":"Kristina C" }> Db.nodes.insert ({"_id": -,"author":"Kristina","text":"... and DBRefs are Easy,too","References":[{"$ref":"Users","$id":"Mike"}, {"$ref":"Nodes","$id": 5}]})Writeresult ({"ninserted":1 })> Db.nodes.insert ({"_id":5,"author":"Mike","text":"MongoDB is fun!"}) Writeresult ({"ninserted":1 })>varNote=db.nodes.findone ({"_id": -})> Note.references.forEach (Function (ref) {Printjson (db[ref.$ref].findone ({"_id":ref. $id})); }){ "_id":"Mike","Display_name":"Mike D" }{ "_id":5,"author":"Mike","text":"MongoDB is fun!"}

MongoDB Learning Note 06

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.