4

I am using MongoDB in our project and I'm currently learning how things work.

When inserting records, MongoDB auto generates the ID. When querying data, it always returns _id. This creates a problem when we deserialise the Bson/Json to out objects as our types don't have that field (and we don't really want to put it in).

Does anyone know how to retrieve data without getting _id in the result?

BGohil
  • 165
  • 1
  • 2
  • 5

1 Answers1

3

Yes, just explicitly omit it:

collection.Find(criteria)
    .SetFields(Fields.Include("oneField", "anotherField").Exclude("_id"))
mookid8000
  • 18,258
  • 2
  • 39
  • 63