2016-08-18 8 views
1

元のデータは、このMongoDBのC#ドライバの投影は名前と値のペアを返し

{ 
"_id" : ObjectId("57b532aefc19a526b4cf7118"), 
"_t" : [ 
    "Product", 
    "ShoppingProduct" 
], 
"name" : "nike jug", 
"createdByUser" : "", 
"updatedAt" : ISODate("2016-08-18T03:59:42.012Z"), 
"url" : "https://localhost:44305/v1/product/1", 
"description" : "a jug from nike", 
"schema" : "ShoppingProduct", 
"sku" : "nikejug001", 
"price" : "15", 
"weight" : "100", 
"brand" : null, 
"manufacturerId" : ObjectId("000000000000000000000000"), 
"entityId" : 1, 
"visibility" : 4, 
"status" : 1, 
"taxClassId" : 2, 
"shortDescription" : "nike jug is good" 
} 

のように見える私はこの

var result = _col.Find("{}").Project("{entityId:1}").ToList(); 

をした私は、戻りデータがこの

{ 
"_id" : ObjectId("57b532aefc19a526b4cf7118"), 
"entityId" : 1 
} 

であることを願っていますしかし私が得たものはこれです:

{ 
    "name": "_id", 
    "value": "57b532aefc19a526b4cf7118" 
}, 
{ 
    "name": "entityId", 
    "value": 1 
} 

db.getCollection('products').find({},{entityId:1})のようなC#ドライバをどうすれば実現できますか?

+0

正しいものは何ですか?これは 'entityId'にのみ投影されるドキュメントです。 – Newton

+0

こんにちは@Newton、コメントありがとう!あなたが結果にもっと近づくならば。私が得たのは、それぞれのプロパティの名前と値のペアです。実際には、私はキーの値のペアです。 'name:entityId、value:1'の代わりに' entityId:1'と言うと – Hao

+0

あなたはC#クラスを追加できますか? – profesor79

答えて

0

これは正常な動作です。公式ドキュメントのReturn the Specified Fields and the _id Field OnlyReturn Specified Fields Onlyを参照してください。

var empty = Builders<BsonDocument>.Filter.Empty; 
var docs = _col.Find(empty).Project("{_id:0, entityId:1}").ToList(); 
foreach (var doc in docs) 
{ 
    if (doc.AsBsonDocument.ElementCount != 1) 
    throw new Exception("Should have only entityId fields"); 
} 
docs = _col.Find(empty).Project("{entityId:1}").ToList(); 
foreach (var doc in docs) 
{ 
    if (doc.AsBsonDocument.ElementCount != 2) 
    throw new Exception("Should have _id and entityId fields"); 
} 
関連する問題