My mongodb-Basic notes

Source: Internet
Author: User
Tags create index findone

A summary of MongoDB knowledge points


1.ObjectId 12 bit 4 time stamp +3 machine identification +2 process id+3 random number

Gettimestamp ()

STR ()


2.Map Reduce performs a large amount of work decomposition and then merges the results into the final result.


3.MongoDB Full-Text Search


CREATE INDEX: Db.posts.ensureIndex ({post_text: "text"})

Use full-Text search: Db.posts.find ({$text: {$search: "Runoob"}})

To delete a full-text index:

Db.posts.getIndexes ()

Db.posts.dropIndex ("Post_text_text")


4. You can use the regular

There are three ways to use regular queries:

Db.posts.find ({post_text:{$regex: "Runoob"}})

Db.posts.find ({post_text:/runoob/})

Title:eval ("/" +title+ "/I")//equal to title:{$regex: title, $Option: "$i"}


5. Fixed-size collections

Features:1> insertion speed very fast 2> query output in order of insertion very fast 3> can eliminate the earliest data when inserting the latest data

Usage:1> store log information 2> cache a small number of documents

Created: Db.createcollection ("Cappedlogcollection", {capped:true,size:10000,max:1000}) fixed size fixed number of documents

Judgment: db.cappedLogCollection.isCapped ()

Conversion: Db.runcommand ({"converttocapped": "Posts", size:10000})

Query: Db.cappedLogCollection.find (). Sort ({$natural:-1})


6. Self-increment

MongoDB is not implemented and can be turned into this function

This function realizes the self-increment of the key

function Getnextsequencevalue (sequencename) {

var sequencedocument = db.counters.findAndModify (

{

Query:{_id:sequencename},

Update: {$inc: {sequence_value:1}},

"New": True

});

return sequencedocument.sequence_value;

}


7.GridFS

Store files that exceed 16M

Information description for Files file

Chunk file Splitter 256k/



8.MongoDb relationship 1:1 1:n n:1 n

1-to-many: introduction-Relational embedded relationships


9. References to databases

{

$ref: Collection Name

$ID: The ID of the reference

$DB: Database name, optional parameters

}

Use:

>var user = Db.users.findOne ({"Name": "Tom Benzamin"})

>var Dbref = user.address

>db[dbref. $ref].findone ({"_id":(dbref. $id)})



10. Use of the overlay index

Db.users.ensureIndex ({gender:1,user_name:1})

Db.users.find ({gender: "M"},{user_name:1,_id:0}) is correct

Db.users.find ({gender: "M"},{user_name:1}). Error



11. Query Analysis

Hint () and explain ()



12.MongoDB Atomic operation

Does not support things, but supports operations of some atoms

。。。 For example, $set $unset $inc $push $pushAll $pull $addToSet of $rename $bit



Use of 13.MonogoDB indexes

CREATE INDEX: Db.users.ensureIndex ({"Tags": 1})

Use index: Db.users.find ({tags: "cricket"})

Verify using: Db.users.find ({tags: "cricket"}). Explain ()

Indexed sub-document fields:

Index created: Db.users.ensureIndex ({"address.city": 1, "address.state": 1, "Address.pincode": 1})

Index use: Db.users.find ({"address.city": "Los Angeles"})

Index purpose: Easy to read, disadvantage additional memory overhead, 2.6 increases the length limit for indexed field values.



14. Basic

MongoDB installation and connection slightly



Create DATABASE: Use Runoob

View all databases: Show DBS

Insert data: Db.runoob.insert ({"Name": "Rookie Tutorial"})

Delete Current database: Db.dropdatabase ()



Delete collection: Db.collection.drop ()

Create collection: Db.cretecollection (name,{capped:true, Autoindexid:true, size:6142800, max:10000})

Capped: Whether fixed

AUTOINDEXID: Whether to create an index with _id

Size: Collection Maximum value

Max: Contains the maximum number of documents in the collection



Inserting documents: Db.col.insert (document)

To update a document:

Db.collection.update (

<query>,

<update>,

{

Upsert: <boolean>

Multi: <boolean>

Writeconcern: <document>

}

)

To delete a document:

Db.collection.remove (

<query>,

<justOne>

)

Query document: Db.col.find (). Pretty ()


Operator Description:

Conditional operator: ><>=<= ...

$type: Data type and number correspondence

Limit and Skip $GT optimizations

Sort ()

Aggregate aggregation operations ...



Pipe: The output of the current command as a parameter to the next command


MongoDB replication: Replicas take over the primary node as the primary node without any downtime

Sharding....


Use in MongoDB Java and node. js ....










My mongodb-Basic notes

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.