【发布时间】:2017-06-12 21:15:56
【问题描述】:
给定一个文档结构:
[{
_id: ObjectId("someId"),
Property: {
CollectionProperty: [{
ItemKey: 28,
StringArray: [
"ItemA",
"ItemB"
]
}]
}
}]
在 mongo shell 中执行以下操作可以满足我的要求:
db.getCollection('collection').updateOne(
{
"_id" : ObjectId("someId"),
"Property.CollectionProperty.ItemKey" : 28
},
{ $push :
{
"Property.CollectionProperty.$.StringArray" : {
$each : [ "AItemA" ],
$sort : 1
}
}
});
但是,我无法在文档或网络上的任何地方找到如何在 C# 代码中重现 $sort 命令。如果它有字段,我可以通过使用“field”:1 来重现,但不是这样的非文档数组。
描述该排序的 mongodb 文档在这里: MongoDB Sort Elements that are not documents
到目前为止,这是我在 C# 中的内容:
Builders<DataType>.Update.PushEach("Property.CollectionProperty.$.StringArray",
new[] { "AItemA" }, null, null, ???)));
???是我不知道该怎么做...有人有什么建议吗?
TIA
【问题讨论】:
-
不是 C 驱动专家,我不会提供建议,我只注意到一件事 { "_id" : ObjectId("someId") "Property.CollectionProperty.ItemKey" : 28 } _id在每个集合中都是唯一的,因此您不必同时过滤 _id 和 Property.xxx
-
@nickmilon 这就是 OP 在他们的
.updateOne()声明中所做的,我相信他们知道这已经为他们工作了。他们只想知道这在 C# 中是如何翻译的。这不是 C BTW。 -
@nickmilon 是的,我只发布了文档结构的一部分——真正的集合属性中可以包含许多项目,但其中的 ItemKey 仅在该特定 _id 内是唯一的......谢谢跨度>
标签: c# arrays mongodb sorting mongodb-.net-driver