3

例如,我在下面有一个收集 = 交付的文档:

{
    "doc": [
        {
            "docid": "15",
            "deliverynum": "123",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        },
        {
            "docid": "16",
            "deliverynum": "456",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        },
        {
            "docid": "17",
            "deliverynum": "999",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        }
    ],
    "id": "123",
    "cancelled": false
}

是否可以使用“deliverynum”= 999 进行搜索,输出如下所示?

{
    "doc": [        
        {
            "docid": "17",
            "deliverynum": "999",
            "text": "txxxxxx",
            "date": "2019-07-18T12:37:58Z"
        }
    ],
    "id": "123",
    "cancelled": false
}

还是我应该为 Doc 部分制作另一个 Collection?

对于这种情况,我无法在 C# 中进行查询。

4

1 回答 1

4

在 Mongo shell 中,您可以使用$(projection)运算符:

db.collection.find({ "doc.deliverynum": "999" }, { "doc.$": 1 })

对应的 C# 代码如下所示:

var q = Builders<Model>.Filter.ElemMatch(x => x.doc, d => d.deliverynum == "999");
var p = Builders<Model>.Projection.ElemMatch(x => x.doc, d => d.deliverynum == "999");

var data = Col.Find(q).Project(p).ToList();

q = Builders<Model>.Filter.Empty如果您想获取所有文档,即使不包含,您也可以使用deliverynum =``999

于 2019-07-18T15:47:20.703 回答