The second part MongoDB additions and deletions to search

Source: Internet
Author: User
Tags array length mongodb query

Learning content:
1.mongodb increased operation
2.mongodb Delete operation
3.mongodb Query Operations
Advanced applications for adding and deleting changes
Capped Collection (fixed set)
Gridfs large file upload or download

1:insert
MongoDB is a document-oriented database with a document structure called Bson (JSON-like)
Db.c1.insert ({name: "user1"});
Db.c1.save ({_id:1, Name: "User1"}); Save () will not be inserted if it is already present, insert if it does not exist
This will be the same value as the ID will not be inserted
Save is updated with the same ID and inserted without the same ID

Delete
Db.c1.remove ();
Db.c1.remove ({name: "user1"});

Db.c1.find ({name: "user1"},{name:1, Age:1, _id:0});
Conditional expressions
< <= > >=! =
$GT: Greater Than
$LT: Less than
$gte: greater than or equal to
$lte: Less than or equal to
$ne: Not equal to


Statistics:
Db.c1.count ();
Db.c1.find (). Count ();
Count (1); There is a condition in it, if it is 1, then the condition will be taken, if 0 will ignore the conditional statement


Sort:
Db.c1.find (). sort (age:1); 1 is ascending 0 is descending

Take how many
Db.c1.find (). limit (4);

Skip a few
Db.c1.skip (1);
Db.c1.skip (0). Limit ($). Sort ({age:-1});

$all
The action is similar to the $in operation, but the difference is that the value of the $all operation requires that the values in the array are all contained in the returned record
Db.c2.find ({post:{$all: [+]}});

$exists
Action checks if a field exists
Db.c2.find ({age:{$exists: 1}}); Test whether a field exists

$mod
Operation allows us to simply do the modulo operation without needing to use the WHERE clause
Here is the value of the field to take the remainder of the operation
Db.c1.find ({age:{$mod: [2]}});


$in operations are similar to in in a traditional relational database
Db.c1.find ({age:{$in: []}})

$nin Contrary to $in
Db.c1.find ({age:{$nin: [2,3,4]}});


$or View records that specify multiple criteria, similar to SQL or
Db.c1.find ({$or: [{name: "user1"},{name: "User2"},{age:10}]});

$nor with $or to filter the specified condition instead
Db.c1.find ({$nor: [{name: "user1"},{name: "User2"},{age:10}]});

Learning content:
1.mongodb increased operation
2.mongodb Delete operation
3.mongodb Query Operations
Advanced applications for adding and deleting changes
Capped Collection (fixed set)
Gridfs large file upload or download

1:insert
MongoDB is a document-oriented database with a document structure called Bson (JSON-like)
Db.c1.insert ({name: "user1"});
Db.c1.save ({_id:1, Name: "User1"}); Save () will not be inserted if it is already present, insert if it does not exist
This will be the same value as the ID will not be inserted
Save is updated with the same ID and inserted without the same ID

Delete
Db.c1.remove ();
Db.c1.remove ({name: "user1"});

Db.c1.find ({name: "user1"},{name:1, Age:1, _id:0});
Conditional expressions
< <= > >=! =
$GT: Greater Than
$LT: Less than
$gte: greater than or equal to
$lte: Less than or equal to
$ne: Not equal to


Statistics:
Db.c1.count ();
Db.c1.find (). Count ();
Count (1); There is a condition in it, if it is 1, then the condition will be taken, if 0 will ignore the conditional statement


Sort:
Db.c1.find (). sort (age:1); 1 is ascending 0 is descending

Take how many
Db.c1.find (). limit (4);

Skip a few
Db.c1.skip (1);
Db.c1.skip (0). Limit ($). Sort ({age:-1});

$all
The action is similar to the $in operation, but the difference is that the value of the $all operation requires that the values in the array are all contained in the returned record
Db.c2.find ({post:{$all: [+]}});

$exists
Action checks if a field exists
Db.c2.find ({age:{$exists: 1}}); Test whether a field exists

$mod
Operation allows us to simply do the modulo operation without needing to use the WHERE clause
Here is the value of the field to take the remainder of the operation
Db.c1.find ({age:{$mod: [2]}});


$in operations are similar to in in a traditional relational database
Db.c1.find ({age:{$in: []}})

$nin Contrary to $in
Db.c1.find ({age:{$nin: [2,3,4]}});


$or View records that specify multiple criteria, similar to SQL or
Db.c1.find ({$or: [{name: "user1"},{name: "User2"},{age:10}]});

$nor with $or to filter the specified condition instead
Db.c1.find ({$nor: [{name: "user1"},{name: "User2"},{age:10}]});

$size
The operation will query an array with an array length equal to the input parameters

$elemMatch the meaning of the element configuration


Slaveok
When we retrieve the replica set, the default route chooses the master machine and we need to turn on the SLAVEOK option when we need to query for any of the slave machines. When we do not turn on the SALVEOK option, the following error will be reported if doing this: * * * not master and Slaveok=false
So we have to do the following: Rs.slaveok (); Enable querying a secondary db.users.find (...)


One) cursors cursor and cursor Methods
Traversing cursors
var x = Db.c1.find ();
X.next ();
X.hasnext ();
while (x.hasnext () = = True) {
X.next ();
}


Null query (with a value of NULL, or a value that does not exist)
Db.c4.find ({age:null}); List all values that are null or not present
Db.c4.find ({age:{$type: 10}}); List all null data

The second type of notation
Db.c4.find ({age:{$exists: 1, $in: [null]}});
Two conditions, one is the exists must exist, and two is the null data

$slice

4. Update
1) Grammar db.collection.update (criteria, objnew, Upsert, Multi)
Parameter description: Criteria: Object used to set the query criteria Objnew: object to set the update content Upsert: If the record already exists, update it, or add a record Multi: if there are multiple records that match the criteria, all updates Note: By default, Only the first qualifying record is updated

$set//Modify fields
Db.c5.update ({name: "user1"},{$set: {age:10}},0,1);

$inc//Add a = a+1 to the field
Db.c5.update ({},{$inc: {age:-10}},0,1);

$unset//Delete a field
Db.c5.update ({},{$unset: {age:1}});

$push
Db.c5.update ({},{$push: {arr:1}}); Press a value into the array


$pushAll
Db.c5.update ({}, {$pushAll: {arr:[2,3,4]}}); Push multiple values into the array

$pop
Db.c5.update ({}, {$pop: {arr:1}}); pops up the last element in the array
Db.c5.update ({}, {$pop: {arr:1}}); POPs the first element in the array

$addToSet
Statement: {$addToSet: {Field:value}}
If filed is an already existing array, and value is not in it, then value is added to the array
Filed does not exist, then store value as an array into the filed
Filed is an existing non-array type, then an error will be made

Extended orientating households
{$addToSet: {a:{$each: [1,2,3,4,5,6]}}};

$pull
Syntax: {$pull: {Field:_value}}
Function: If field is an array, delete records that match the _value search criteria
If field is a non-array that already exists, the error will be

$pullAll
Syntax: {$pullAll: {Field:value_array}}
Function: Similar to $pull, except that the data type of value is an array

$rename
Syntax: {$rename: {old_field_name:new_field_name}}
Function: Rename the specified field name

Special operators: $
The $ operator Code queries the record entry for the first match condition in a record
$db. C5.update ({"Arr.title": "Linx"}, {$set: {arr.$.title: "I Lover Linx"}});

Note: In the array with the $unset operator, the effect is not the deleted element, but the matching element becomes null
$db. C5.update ({"Arr.title": "PHP"},{$unset: {arr.$:1}});


var x = Db.c1.findOne ({"User": "user1"});
X.age = 40;
X.nickname= "XLC";
Db.c1.save (x);


Performance monitoring
By monitoring the performance of the database, we can better understand the working state of the database and optimize it from the page.
Introduction to several performance monitoring tools
Mongofniff
This tool can be monitored from the bottom up in the end there are those lawsuits sent to MongoDB to execute
./mongosniff--soruce NET Lo
It is a real-time dynamic monitoring, you need to open a client for the action, you can export this data to a log file, then you can keep the history of all the database operations, for later performance analysis and security audit work will be a huge contribution

The second part MongoDB additions and deletions to search

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.