search

Home  >  Q&A  >  body text

c# - How does the Find() method in MongoDB.NET return the value of the specified field?

Using ASP.NET MVC5, I want to filter and read specified data from mongoDB based on conditions, similar to a relational database select fields1,fields form table where name='123'

How to write a method that returns a specific field when operating mongoDB in .NET? I can't find a suitable solution by looking at the documentation or searching, please help everyone

高洛峰高洛峰2792 days ago852

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-02 09:24:44

    Although sometimes the documentation is incomplete, you should be able to find the answer to your question directly from the documentation, but you may not be looking in the right place.
    Select the driver version:
    https://api.mongodb.com/csharp/
    The projection method is in the MongoCursor里面,而不是Findmethod

    public virtual MongoCursor SetFields(
        params string[] fields
    )

    Like a series of other methods, it still returns MongoCursor, so you can actually do cascading operations on it, such as

    collection.Find(...)
        .SetFields(...)
        .SetSortOrder(...)
        .SetSkip(...)
        .SetLimit(...)
    

    reply
    0
  • Cancelreply