Windows MONGODB Basic Gameplay series two curd operations (create, update, read and delete)

Source: Internet
Author: User
Tags bulk insert documentation

Windows MongoDB Basic Gameplay series
    • Introduction and installation of the Windows MongoDB Basic gameplay series
    • Windows MONGODB Basic Gameplay series two curd operations (create, update, read and delete)
Simply say a few words in MongoDB 3 elements: db (Database), collection (collection), document (documentation)
Where collection is similar to a table in a database, document is similar to a row, so that we can compare the contents of memory learning. Data format MongoDB documents is a binary form of the Bson format (a kind of JSON in the storage format)
Insert Operation-c① Single insertion
Db.testData.insert ({"Name": "A", "Age": 18})

② BULK INSERT (directly is a JS loop, so it seems to be not very cool, will JS should be able to play a very good turn to MongoDB look, it seems I have to work a bit)

for (var i = n; I < i++) {
Db.testData.insert ({"Name": String.fromCharCode (i+47), "Age": i})
}

Update operation-u① Overall updates (replaces the entire document with values other than _id, with a completely new document as the second parameter of the update)
Db.testData.update ({"Name": "K"},{"name": "K", "Age": 28})

② Partial update (update a specific field in a document, here's just a look at the $inc/$set)
    • $inc can increase or decrease the value of one of the document's numeric values.
    • Db.testData.update ({"Name": "K"},{$inc: {"Age": 72}})
    • $set Update the value of a field in a document, or the value of an embedded document in a document
    • Db.testData.update ({"Name": "K"},{$set: {"Age": 28}})
③upsert (By default, update () does nothing if it does not match to the document, but if it is upsert (that is, when the third parameter of update is true), when no match is made to the document, the contents of the second parameter are inserted as a new document)
Db.testData.update ({"Name": "L"},{"name": "L", "age": 29},true)

④ batch update (this is also the problem that we often meet, but can think of the solution or circular change, supposedly this efficiency is relatively low, such as the subsequent documentation update this related method, hope to see this problem after a good solution friends left a little comment, Thank you) The Find Operation-rmongodb provides the Db.collection.find () method for querying operations, simply by selecting document in collection. That is, we usually do the same thing in a table.
① $gt, $gte, $lt, $lte, $ne, no special (>,>=,<,<=,!=,=)
Demo:db.testData.find ({"Age": {$gt: $}}) Db.testData.find ({"Age": {$gte: $}}) Db.testData.find ({"Age": {$lt: 20}}) Db.testData.find ({"Age": {$lte: $}}) Db.testData.find ({"Age": {$ne: $}}) Db.testData.find ({"Age": 20})
② $and, $or, $in, $nin (And,or,in,not in)
Demo:db.testData.find ({$and: [{"Name": "A", "Age":]}) Db.testData.find ({$or: [{"Name": "A", "name": "B"}]}) Db.testData.find ({"name": {$in: ["A", "B"]}}) Db.testData.find ({"name": {$nin: ["A", "B"]}})
Remove operation-D in MongoDB, Db.collection.remove () is the removal of a document from a collection. Delete all Documents
Db.testData.remove ({})
The above method simply removes all the documents from the collection, the collection itself is still present, and if you want to delete the collection directly, you can delete the eligible document by dropping () using a more effective method.
Db.testData.remove ({"Name": "A"})

This article original blog address: http://www.cnblogs.com/unofficial official website address: www.pushself.com

Windows MONGODB Basic Gameplay series two curd operations (create, update, read and delete)

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.