有没有办法从_id字段中从MongoDB检索数据? [英] Is there a way to retrieve data from MongoDB without the _id field?

查看:64
本文介绍了有没有办法从_id字段中从MongoDB检索数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在项目中使用MongoDB,目前正在学习如何工作.

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

插入记录时,MongoDB自动生成ID.查询数据时,它始终返回_id.当我们对Bson/Json进行反序列化以取出对象时,这会产生一个问题,因为我们的类型没有该字段(并且我们真的不想放入它).

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).

有人知道如何在不获取_id的情况下检索数据吗?

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

推荐答案

是的,只需将其显式省略即可:

Yes, just explicitly omit it:

collection.Find(criteria)
    .SetFields(Fields.Include("oneField", "anotherField").Exclude("_id"))

这篇关于有没有办法从_id字段中从MongoDB检索数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆