MongoDB for node. JS developers seven-week study-final Test summary (1)

Source: Internet
Author: User
Tags createindex

Many free courses are available on the MONOGODB website, and the "MongoDB for node. JS Developers" Learning is selected. Course is in English, but there are Chinese subtitles ~, after each course there are exercises. The seven-week course passed quickly and learned a lot. With the help of the final test of the problem, do a summary, save oneself soon forget (eh, memory poor =)

The final Test a total of 10 questions, the contents of the study are: 1, query 2, aggregation 3, update $addtoset 4, update $inc 5, index 6, add optimization 7, cursor 8, copy 9, TAB key 10, Index explain properties

1. Enquiry

function(query, fields, limit, skip, batchsize, options) {varcursor =NewDbquery ( This. _mongo, This. _db, This ,                         This. _fullname, This. _massageobject (query), fields, limit, skip, batchsize, Options | | This. Getqueryoptions ()); varConnobj = This. Getmongo (); varReadprefmode =Connobj.getreadprefmode (); if(Readprefmode! =NULL) {cursor.readpref (Readprefmode, Connobj.getreadpreftagset ()); }    returncursor;}

From the function above you can see that the Find function accepts 6 parameters, query criteria, fields specify the key to return, limit limits return results, skip skips x query results, batchsize set return batch number, option other settings.

Query criteria can be queried for greater than ($gt), less than ($lt), or queries, and so on. For detailed usage, see official website.

MONGO can also query the array, the usage is not difficult, such as the query of a list contains a want to find content, and the normal query is not any different, then find the content is a subset of the array. Use $allif you want to fully match the results of your query criteria. Use $inIf the result of the query contains arbitrary query criteria.

Most of the time we know what we're looking for in the position of the array, you can use a subscript-like method to find it. Like

Db.inventory.find ({'memos.0.by': ' Shipping '})

0 represents the first position in the array, and the following ". By" refers to the embedded document in the query, which is the dot syntax provided by MONGO.

2. Aggregation

Aggregation allows us to analyze the data, which uses the concept of a pipeline, that is, to set up a sequence of tasks performed in a set of arrays, and the result of each task is the input of the next task.

Borrow a picture of the official website:

The above aggregation first matches the document with status equal to a, then groups according to CUST_ID and adds total to the amount of each group.

A total of filter pipe operators are provided $match, $limit, $skip, sort pipe operators $sort, grouping operators $group, casting operators $project and split operators $unwind , and so on.

3. Update $addToSet

Using update directly is an overall coverage update, and we often just update a portion of the document. Commonly used $set modifiers, when we want to add content to an array can use $push, but $push simply add to the end of the array, if we want to ensure that the contents of the array of the uniqueness of the words should use $ Addtoset

// Syntax {$addToSet: {<field1>: <value1>, ...}}; // Example db.test.update (   1 },   "C", "D" ]}})

4. Update $inc

$inc modifier is used to increase an existing key value, or to create one if it does not exist.

I have some problems on this problem. I need to update the document for a given position within an array, where the position is a variable, assuming that the variable name is comment_ordinal

You can update the Array.0.foo as mentioned above

{$inc: {array.0.foo:1}}

But you can't update them like this

{$inc: {Array.comment_ordinal.foo: 1};            // Direct Error {$inc: {"Array.comment_ordinal.foo": 1};          // a array:[{comment_ordinal.foo:1} is created,.....]

Then I was stuck here .... Now thinking is also limited by the frame frame.

{$inc: {<field1>: <amount1>, <field2>: <amount2>,...}}

After seeing this kind of writing, I forgot {<field1>: <amount1>, <field2>: <amount2>,. ...} This thing is an object, too!!!

var selector = {}; selector[' comments. ' + comment_ordinal + '. Num_likes '] = 1; Posts.update ({  selector},.......)

You can put {array.0.foo:1} whole package up ~, so OK.

5. Index

Indexes can provide more efficient lookups, but there are times when the opposite is true.

There are 3 basic types of indexes: Single-button index, composite index, multi-key index

Single-key index: Db.friends.createIndex( { "name": 1 } )

composite index: db. Products. Createindex ({ "item" : 1: 1 )

Multi-key index: If one of the keys in the index is an array in a document, then this is automatically indexed by MongoDB as a multi-key and does not require additional settings. This can be very efficient when querying an array (the official web is so written, not practiced)

MongoDB for node. JS developers seven-week study-final Test summary (1)

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.