How to realize data query operation in MongoDB
Today, I will talk to you about how to achieve data query operation in MongoDB, many people may not know much about it. In order to make you understand better, the editor has summarized the following content for you. I hope you can get something according to this article.
In any database, the query operation of data is the most troublesome, while in MongoDB database, the support for query is very good.
Syntax: "db. Collection name .find ({query condition} [, {set displayed field}])"
Example: the simplest use is to use the find () function directly to complete the query
> db.info.find ()
Example: want to query the data whose url is "www.stone.com"
> db.info.find ({"url": "www.stone.com"})
{"_ id": ObjectId ("5990f8ea3268c8e84253ba3b"), "url": "www.stone.com"}
{"_ id": ObjectId ("5990f97d3268c8e84253ba3c"), "url": "www.stone.com"}
It is found that the equality relationship is also set in the form of JSON when querying data. It is impossible to do without JSON data throughout its development.
Strictly speaking, the set display field is called the data projection operation, if you do not need to display the field setting "0", but need to display the field setting "1".
Example: do not want to display "_ id"
> db.info.find ({"url": "www.stone.com"}, {"_ id": 0})
{"url": "www.stone.com"}
{"url": "www.stone.com"}
> db.info.find ({"url": "www.stone.com"}, {"_ id": 0, "url": 1})
{"url": "www.stone.com"}
{"url": "www.stone.com"}
At the same time, the query for data can also use the "pretty ()" function to format the output.
Example: formatted display
> db.info.find ({"url": "www.stone.com"}, {"_ id": 0, "url": 1}) .pretty ()
{"url": "www.stone.com"}
{"url": "www.stone.com"}
Example: query individual data
> db.info.findOne ()
{"_ id": ObjectId ("5990f8ea3268c8e84253ba3b"), "url": "www.stone.com"}
After reading the above, do you have any further understanding of how to implement data query operation in MongoDB? If you want to know more knowledge or related content, please follow the industry information channel, thank you for your support.