What is the method for querying a field in MongoDB?

To query a field in MongoDB, you can use the following method:

db.collection.find({fieldName: value})

In this case, db.collection is the collection to be queried, fieldName is the name of the field to be queried, and value is the value to be matched. This will return all the documents containing the specified field and value.

For example, if you want to search for a document with a field named “name” and a value of “John,” you can write it like this:

db.users.find({name: "John"})

This will return all documents in the collection where the value of the name field is John.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds