MongoDB the use of experience simple summary _mongodb

Source: Internet
Author: User
Tags array length mongodb

1.mongodb Features

1 MONGO is a document-oriented database that brings together the features of both NoSQL and SQL databases.
2 All entities are created on first use.
3 does not have strict transaction characteristics, but it ensures that any data changes are atomic in nature.
4) There is no fixed data model
5) MONGO uses JavaScript as the command line execution engine, so it is slow to use the shell for complex computations and queries.
6) MONGO itself supports clustering and data fragmentation
7 MONGO is implemented in C + +, supporting mainstream operating systems such as Windows Mac Linux
8) Superior performance and fast speed

2.mongo Common operation

1. Additions and deletions operation

    1. Db.user.insert ({name: ' AAAA ', age:30});
    2. Db.user.save ({name: ' AAAA ', age:30});
    3. Db.collection.insertOne ({});(3.2 new features)
    4. Db.collection.deleteOne (<filter>,{});(3.2 new features)
    5. Db.collection.remove ({name: ' AAA '});
    6. Db.collection.remove ();(Delete all)

2. Update operation

    1. Db.users.update ({"Name": "Joe"}, Joe);
    2. Db.users.update ({"Name": "Joe"}, Joe, true);------Upsert Mode
    3. Db.users.update ({"Name": "Joe"}, Joe, True, true);------Multi Mode

Update is the first document to replace the document instead of the local modification of the default update update, multi mode updates all matching

3. Query operation

--General enquiries

    1. Db.user.find ();
    2. Db.user.find ({name: ' AAA '});
    3. Db.user.findOne ({name: ' AAA '});

--Fuzzy query

    1. Db. Userinfo.find ({userName:/a/}) (name%a%)
    2. Db. Userinfo.find ({userName:/^a/}) (name a%)

4. Operator

1. $lt, $lte, $gt, $gte (<=, >=)
2. $all the elements in the array exactly match Db.things.find ({a: {$all: [2, 3]}});
3. $exists Optional: True,false db.things.find ({A: {$exists: true}});
4. $mod modulo: a% = = 1 Db.things.find ({A: {$mod: [10, 1]}});
5. $ne counter: That is not equals Db.things.find ({x: {$ne: 3}});
6. $in similar to SQL in Operation Db.things.find ({j:{$in: [2,4,6]}});
7. $nin $in reverse operation, that is, SQL not in Db.things.find ({j:{$nin: [2,4,6]}});
8. $nor $or reverse operation, which does not match (A or B) db.things.find ({name: "Bob", $nor: [{a:1},{b:2}]})
9. $or or clause, note that $or cannot be nested using Db.things.find ({name: "Bob", $or: [{a:1},{b:2}]})
$size Match Array length db.things.find ({A: {$size: 1}});
$type match the data type of the subkey, please see Db.things.find ({A: {$type: 2}}) for details;

5. Array query

$size used to match the array length (that is, the maximum subscript)
Returns a document that contains 5 elements comments
Db.posts.find ({}, {comments:{' $size ': 5}});
Using redundant fields to implement
Db.posts.find ({}, {' Commentcount ': {' $gt ': 5}}});
The $slice operator is similar to a subkey filter, except that it filters the items in the array
Returns only the first 5 items in an array
Db.posts.find ({}, {comments:{' $slice ': 5}});
Returns only the last 5 items in an array
Db.posts.find ({}, {comments:{' $slice ':-5}});
Skip the Top 20 items in the group and return the next 10 items
Db.posts.find ({}, {comments:{' $slice ': [20, 10]}});
Skip the last 20 items in the group and return the next 10 items
Db.posts.find ({}, {comments:{' $slice ': [-20, 10]}});
MongoDB allows you to specify the subscript of an array in a query for a more precise match
Returns all documents for Abe for the by subkey of item 1th in comments
Db.posts.find ({"comments.0.by": "Abe"});

3. Use of indexes

1. Create an index

Db.things.ensureIndex ({' J ': 1});
To create a child document index
Db.things.ensureIndex ({' User. Name ':-1} ';
Create a composite index
Db.things.ensureIndex ({
' J ': 1,//Ascending
' x ':-1//Descending
});
If your find operation uses only one key, the index direction is irrelevant
When creating a composite index, it is important to carefully consider the direction in which each key is sorted

2. Modify the Index

Modify the index only to rerun the index command
If the index already exists, it will be rebuilt and no indexes will be added
Db. Things. Ensureindex ({
---The original index will be rebuilt
' User. Name ':-1,
---Add an ascending index
' User. Name ': 1,
---Create a new descending index for age
' User. Age ':-1
},
Open Background execution
{' Background ': true}
);
Rebuilding indexes
Db. Things. ReIndex ();

3. Delete Index

Delete all indexes in the collection
Db. Things. Dropindexes ();
Deletes the index of the specified key
Db.things.dropIndex ({
X:1,
Y:-1
});
Use command to delete the index of the specified key
Db.runcommand ({
Dropindexes: ' foo ',
Index: {y:1}
});
Delete all indexes using command
Db. RunCommand ({dropindexes: ' foo ', Index: ' * '})
Deleting all the documents (remove) in the collection does not affect the index, and the index is rebuilt when a new document is inserted.

4. Unique index

Creates a unique index, which is also a unique index
Db.things.ensureIndex (
{
' FirstName ': 1,
' LastName ': 1
},   {
Specify as unique index
' Unique ': true,
Delete duplicate records
' Dropdups ': true
});

5, mandatory use of the index

Mandatory use of indexes A and b
Db.collection.find ({
' A ': 4,
' B ': 5,
' C ': 6
}). Hint ({
' A ': 1,
' B ': 1
});
Force no index to be used
Db.collection.find (). Hint ({
' $ Natural ': 1
});

Index Summary:

    1.     indexes can speed up queries;
    2.     Individual indexes do not care about their index direction;
    3.     Multi-key indexes need to be cautious Re-consider the direction of each index;
    4.     You should uninstall all indexes when you make mass data updates, and then rebuild the index after the data update is complete;
    5.     do not attempt to create an index for each key , consider the actual needs, not the more indexes, the better;
    6.     Unique indexes can be used to eliminate duplicate records;
    7.     geo-spatial indexes are not units, Its internal implementation is the basic Pythagorean theorem algorithm
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.