티스토리 뷰

Projects/MongoDB

MongoDB cheat sheet

hackability 2015. 5. 26. 11:33

This post is a cheat sheet for MongoDB. This cheat sheet is not technical, maybe you can use this when you cooking.



# Find all data in collection

Usage:

db.collection.find({})



# Find if it has specific key

Syntax: $exists

{<Field>: {$exists: <boolean>}}


Usage:

db.collection.find({"field": {"$exists": true}})


Reference:

http://docs.mongodb.org/manual/reference/operator/query/exists/



# Delete Key ( $unset )

Syntax: $unset

{$unset : {<Field>: "", ...}}


Usage:

db.collection.update({"field": condition}, {"$unset": {"del_key_field": ""}})

-> In unset operation, empty string value of "del_key" key means nothing


If you use it then delete only one document. So, if you want to delete multiple document which satisfied by the condition, then setting upsert as False and multiple as True like this


db.collection.update({"field": condition}, {"$unset": {"del_key_field": ""}}, false, true)


The 3rd option is upsert (False), and the 4th option is multiple.


Reference:

http://docs.mongodb.org/manual/reference/operator/update/unset/






댓글