C#-MongoDB-更新嵌套文档中的元素 [英] C# - MongoDB - Update an element inside a Nested Document
问题描述
我有如下MongoDB文档
I have a MongoDB Document as follows
{
"_id" : ObjectId("5a55775cbd12982cc063c71a"),
"ShipmentNumber" : "00004000000048652254",
"Cartons" : [
{
"_id" : ObjectId("5a5575bcbd12982cc063b718"),
"CartonNumber" : "0076013926580S",
"Skus" : [
{
"_id" : ObjectId("5a5575bcbd12982cc063b719"),
"SkuNumber" : "06577647",
"ShippedQuantity" : 12,
},
{
"_id" : ObjectId("5a5575bcbd12982cc063b519"),
"SkuNumber" : "06577657",
"ShippedQuantity" : 15,
}
],
"IsScanned" : false,
},
}
如何根据C#代码中的"_id"为特定的Sku元素更新"ShippedQuantity"?
How can I update the "ShippedQuantity" for a particular Sku element based on its "_id" in C# code ?
我尝试了以下类似方法.但这不起作用. 收到类似
I tried something like below. But it is not working. Getting error message like
无法使用零件(纸箱纸箱.$ [].Skus.$.ShippedQuantity)来 遍历元素
cannot use the part (Cartons of Cartons.$[].Skus.$.ShippedQuantity) to traverse the element
var filter = Builders<BsonDocument>.Filter.Eq("Cartons.Skus._id", new ObjectId("5a5575bcbd12982cc063b519"));
var update = Builders<BsonDocument>.Update.Set("Cartons.$[].Skus.$.ShippedQuantity", 50)
当我尝试更新多级文档时遇到了困难. (在这种情况下,我有一个纸箱列表,每个纸箱都有其自己的skus列表,我需要更新特定的sku元素)
I am facing difficulties when I try to update multi level documents. (In this case I have a list of Cartons and each carton will have its own list of skus and I need to update a specific sku's element)
请提供解决方案或替代方法,以使用C#在MongoDB中更新此内部级别(超过2个级别)的文档.
Please provide a solution or alternative approach to update this inner level (more than 2 levels) documents in MongoDB using C#.
我将MongoDB服务器更新为最新的3.6.1.但这也无济于事.
I updated my MongoDB server to the latest 3.6.1. But that is also not helping.
感谢您的帮助.
推荐答案
首先,您需要在MongoDB中运行此命令以应用版本3.6.1 db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
First, you need to run this command in your MongoDB to apply the new features of version 3.6.1 db.adminCommand( { setFeatureCompatibilityVersion: "3.6" } )
以下是该更新所需的代码:
Here is the code you need for that update:
var filter = Builders<YOUR_CLASS>.Filter.Eq("_id", new ObjectId("5a55775cbd12982cc063c71a"));
var update = Builders<YOUR_CLASS>.Update.Set("Cartons.$[i].Skus.$[j].ShippedQuantity", 50);
var arrayFilters = new List<ArrayFilterDefinition>
{
new BsonDocumentArrayFilterDefinition<Setup>(new BsonDocument("i._id", new ObjectId("5a5575bcbd12982cc063b718"))),
new BsonDocumentArrayFilterDefinition<Setup>(new BsonDocument("j._ID", new ObjectId("5a5575bcbd12982cc063b719")))
};
var updateOptions = new UpdateOptions { ArrayFilters = arrayFilters };
var (updated, errorMessage) = await UpdateOneAsync(filter, update, updateOptions);
此外,您可以在MongoDB中运行以下设置来查看最终查询,并在RoboMongo或Studio 3T中手动运行它们以调试它们:
Additionally, you can run set these settings in your MongoDB to look at your final queries and run them manually in RoboMongo or Studio 3T to debug them:
db.setProfilingLevel(2) -> to view query logs under C:\data\log\mongod.log
db.setLogLevel(5) -> to view query logs under C:\data\log\mongod.log
在日志文件中查找"UPDATE"查询.之后,您可以将日志设置重置为0
look for the "UPDATE" query in the log file. After that, you can reset the log setting back to 0
db.setProfilingLevel(0)
db.setLogLevel(0)
I've had the same problem and asked the same question Here Have a look at it.
这篇关于C#-MongoDB-更新嵌套文档中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!