Characteristics
Every document in a modeless MongoDB is a JSON object, so you don't have to define a collection's structure in advance, and each document in the collection can have a different structure.
Asynchronously writes to MongoDB by default all writes are "unsafe", that is, when a request is received by MongoDB, the write operation is completed and a "successful" response is returned. This is the default behavior, and of course you can set some options so that the operation waits for the write to complete before returning the response. Just for most applications, this "unsafe" is safe enough.
Simple query MongoDB only supports simple queries, MongoDB only stores data, many other logic should be solved in the application. MongoDB therefore has a simple and highly consistent API interface in each programming language.
No operational mongodb almost no option can be set, the cluster is also set up once can be self-active to solve the problem, very little need maintenance.
Install and Backup execute Mongod startup database, execute MONGO start client Shell,.
MongoDB is designed to perform on a 64bit operating system, and in 32bit cases the maximum data file limit is 2G.
MongoDB does not write changes to the disk in real time when it executes, so it needs to write all the data to disk when the server is shut down. When the server suddenly loses power, MongoDB database file will be corrupted, need to repair talent to execute again, this process will lose power when the write operation. When you are backing up MongoDB in progress, you need to use your own Mongodump tool and not copy its database files directly.
Design document Structure
Objectidmongodb will default to each document by adding a field named _id, type Objectid, which uniquely identifies each document. This ID is generated by time, server, process number, and can even feel like the only one in the world.
In addition to MongoDB's feeling that _id joins ObjectID, you can create your own ObjectID in the document to uniquely identify an object's functionality.
Query and update the so-called "additions and deletions" in MongoDB corresponding:
Find/findone: Query
Update/save: Change
Insert/save: New
Remove: Delete
In MongoDB, the operator begins with $ and is divided into three main categories: The query operator, the update operator, and the aggregate query operator.
Query operators are used for queries in find and update, such as $GT (greater than), $ne (not equal to), $in (one of several values matched), logical operations: $and, $not.
The update operator is used for updates in the update, such as $inc (increment the number), $set (part of the document is changed), $unset (delete a field).
MongoDB has good support for embedded documents and arrays: $addToSet (adding elements to the collection), $push (adding elements to an array), $pull (removing elements from an array).
Aggregate queries (Aggregation) are similar to the GROUP in SQL databases, providing statistical and computational capabilities that are much more powerful, after all, able to execute JavaScript code directly in the database, just because of the performance of the relationship, is not suitable for frequent calls in the application.
There are several options for the query command limit: Limits the number of results returned
Skip: Skip Some results
Sort: Sorting the results
Fields: Returns only the specified field
MongoDB can conveniently put some of the data that MySQL needs to be associated with a one-to-many relationship through an array + object as a document stored in a collections, and provides a variety of APIs to support the operation of an array within a document.
MongoDB often uses query methods with the embodiment in node:
1. Database connection mongodb:mongo-u username-p password Host:port/dbs
Node+mongoskin:require ("Mongoskin"). db (Username:[email protected]:p Ort/dbs);
2. Index
Person.ensureindex ({"Name": 1},{"unique": true}, callback);
The first parameter is selector, the second is the option, and the MONGODB index option is unique (unique index).
Ensureindex first looks for the existence of this index, assuming that it does not exist, then indexes it, so there is no case of re-establishment.
3. New/Add Data Person.save ({"Name": "Jobs", age:32, Gender: "Male"}, callback);
Person.save ({"Name": "Tim", age:29, Gender: "Male"}, callback);
Db. Collection name. Insert ({name: ' Xiaoxiao ', age:2},function (Error,result) {});
4. Enquiry
FindOne method: Query conforms to the results of selector conditions, take the first to Callback,err is the error message, assuming there is no error, then the Undefined,data data for a JS object.
Person.findone ({"Name": "Jobs"}), CallBack (err, data));
Find method: The query conforms to the selector condition, but also can increase a series of parameters, and finally through the ToArray method, the data generation array way to callback.
Person.find ({"Gender": "Male"},{sort:[[' name ', 1]],skip:pagenum* (page-1), limit:pagenum}). ToArray (Callback (Err, Data) {});
At the same time the query can be $in, $GT, $lt and other ways to determine the scope of selector.
$ne: Not equal;
Personcollection.find ({age:{$ne: ' 2 '}},function (error,result) {}
$in: Whether a value is specified in this range, followed by a general array of ranges, the type of the array in the value can be different;
Personcollection.find ({age:{$in: [' 2 ', ' 3 ', ' 4 ', ' 5 ']}},function (error,result) {}
$nin: Indicates that it is not in an array range;
Personcollection.find ({age:{$lte: ' 3 '}},function (error,result) {}
$lt, $lte, $GT, $gte: Represent < respectively, <=,>, >=, which are used to set the query scope;
$or: Also specify a range to use, but the biggest difference with the above is that $in, $nin have and only one array range, and $or can be multiple array scope to meet the conditions;
Personcollection.find ({$or: [{name: ' King two '},{age: ' 2 '}]},function (error,result) {}
5. Update
There are two points to note:
[1], the Update method is assumed to be updated in the document of one or several properties, it must be in the form of {$set: {age:33}}, assuming the form of {age:33}, then the other contents of this document will be deleted, only {age:32} This property is left.
[2], the Update method by default is only updated to find the first document, assuming that all the documents are updated, you need to add {multi:true} in the option section, the false idea to find this record is not found when a new one, you want to add {upset in the option section: True}.
Person.update ({"Name": "Jobs"}, {$set {"Age": +}}, {multi:true}, callback (ERR))
[3], $inc added by step, for example, to add 1 to the Age field.
Personcollection.findandmodify ({_id: ' 1111111 '},[],{$inc: {age:1}},{new:true,upset:true},function (Error,result) { }
6. Delete
Person.remove ({"Name": "Jobs"},callback (err) {});
7, selector in the use of MongoDB self-generated _idmongodb will generate a _id property for each document, similar to the MySQL primary key, is unique. The type of _id is the MongoDB custom Objectid type, so although it is possible to get a 12-bit or 24-bit _id string at the time of the query, the false idea is to find or otherwise operate through _id in selector. You must first type-convert through the Db.collection.id () method.
Person.remove ({"_id":p erson.id (Stringid)}, callback (ERR) {});
8, MongoDB operation of the array in the document (through the Update method) [1], through the $addtoset method to insert records into the array, before inserting the method will first look for the existence of this record, assuming that there is not inserted; If you want to insert repeated values, you can Push to join.
Person.update ({"Name": "Jobs"}, {$addToSet: {
Company: {
Name: "Google",
Address:usa,
Workingtime:3
}
}, function (Err) {});
[2], change the data in the array: by the $ character. If you want to match two attributes when querying in an array, you will need to use the $elemmatch method, assuming that only {"name": "Google", "Address": USA} to find, Will select all the elements named Google or address for the USA. After locating this element, select it by $ to change it.
Person.update ({
"Name": "Jobs",
company:{$elemMatch: {"name": "Google", "Address": USA}}
}, {$set: {"Company.$.workingtime": 4}},function () {})
[3], delete the data specified in the array: through the $pull method
Person.update ({
"Name": "Jobs",
},{$pull: {company:{"name": "Google", "Address": "USA"}}},function (err) {})
$pop: Delete the value of the array header or tail, {$pop: {filed:-1}} from the beginning, {$pop: {filed:1}} removed from the tail.
Personcollection.findandmodify ({_id: ' 1111111 '},[],{$pop: {tag:-1}},{new:true,upset:true},function (Error,result) {}
Update
July 19, 2014 15:11:23
The two points of attention that are actually encountered in the project recently:
1. You cannot perform two operations on the same attribute field at a time;
2, $inc or $set, such as operation, not when empty.
References http://jysperm.me/technology/1575
Http://www.cppblog.com/dead-horse/archive/2011/09/23/156617.html
Https://github.com/kissjs/node-mongoskin
Document Information
- Copyright Disclaimer: Free Reprint-Non-commercial-non-derivative-retain attribution | Creative Commons by-nc-nd 3.0
- Original URL: http://blog.csdn.net/cdztop/article/details/31324027
- Last modified: July 19, 2014 15:12
MongoDB + Node-mongoskin Simple Demo sample