MongoDB - 如何将嵌入式数组推送到现有文档中
MongoDB - How to push an embedded array into an existing document
型号:
[BsonIgnoreExtraElements]
public class UserPortfolioList
{
public string? Username { get; set; }
public List<Pflist>? Pflist { get; set; }
}
public class Pflist
{
public string PfName { get; set; } = "DEFAULT NAME";
public List<string>? Symbols { get; set; }
}
现有MongoDB个文档:
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH A",
"symbols": [
"GOOG",
"MSFT"
]
}
]
}
我要推送的数组(相同的用户名,但不同的 pfName 和符号)
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
预期结果:
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH A",
"symbols": [
"GOOG",
"MSFT"
]
},
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
代码:
public async Task UpdateUserPfAsync(UserPortfolioList userPflist)
await _UserPortfoliosCollection.UpdateOneAsync(Builders<UserPortfolioList>.Filter.Eq(
"Username", userPflist.Username),
Builders<UserPortfolioList>.Update.Push(x => x.Pflist, userPflist.Pflist));
编译器抛出错误:
can not convert lambda express to type......
我几乎尝试了 Whosebug 上的所有方法,但是 none 对我有用。
请帮忙。谢谢!
关注:
由于您要将单个对象添加到 pflist
,因此您应该使用 userPflist.Pflist[0]
而不是 userPflist.Pflist
,因为 userPflist.Pflist
将导致结果:
{
username: "aaa",
pflist: [
0: Object
1: Array
0: Object
]
}
将单个元素推送到数组
MongoDB查询:
db.collection.update({
username: "aaa"
},
{
$push: {
"pflist": {
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
}
})
Sample Mongo Playground (Push single element to the array)
MongoDB C#:
await _UserPortfoliosCollection.UpdateOneAsync(
Builders<UserPortfolioList>.Filter.Eq("Username", userPflist.Username),
Builders<UserPortfolioList>.Update.Push(x => x.Pflist, userPflist.Pflist[0]));
注意:.Push()
的 lambda 表达式在 值是对象 而不是数组时有效。
将多个元素压入数组
要将多个元素压入数组,您需要一个 $each
运算符。
MongoDB查询:
db.collection.update({
username: "aaa"
},
{
$push: {
"pflist": {
$each: [
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
}
})
MongoDB C#:
await _UserPortfoliosCollection.UpdateOneAsync(
Builders<UserPortfolioList>.Filter.Eq("Username", userPflist.Username),
Builders<UserPortfolioList>.Update.PushEach(x => x.Pflist, userPflist.Pflist));
Update Result
型号:
[BsonIgnoreExtraElements]
public class UserPortfolioList
{
public string? Username { get; set; }
public List<Pflist>? Pflist { get; set; }
}
public class Pflist
{
public string PfName { get; set; } = "DEFAULT NAME";
public List<string>? Symbols { get; set; }
}
现有MongoDB个文档:
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH A",
"symbols": [
"GOOG",
"MSFT"
]
}
]
}
我要推送的数组(相同的用户名,但不同的 pfName 和符号)
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
预期结果:
{
"username": "aaa",
"pflist": [
{
"pfName": "US TECH A",
"symbols": [
"GOOG",
"MSFT"
]
},
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
代码:
public async Task UpdateUserPfAsync(UserPortfolioList userPflist)
await _UserPortfoliosCollection.UpdateOneAsync(Builders<UserPortfolioList>.Filter.Eq(
"Username", userPflist.Username),
Builders<UserPortfolioList>.Update.Push(x => x.Pflist, userPflist.Pflist));
编译器抛出错误:
can not convert lambda express to type......
我几乎尝试了 Whosebug 上的所有方法,但是 none 对我有用。
请帮忙。谢谢!
关注:
由于您要将单个对象添加到 pflist
,因此您应该使用 userPflist.Pflist[0]
而不是 userPflist.Pflist
,因为 userPflist.Pflist
将导致结果:
{
username: "aaa",
pflist: [
0: Object
1: Array
0: Object
]
}
将单个元素推送到数组
MongoDB查询:
db.collection.update({
username: "aaa"
},
{
$push: {
"pflist": {
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
}
})
Sample Mongo Playground (Push single element to the array)
MongoDB C#:
await _UserPortfoliosCollection.UpdateOneAsync(
Builders<UserPortfolioList>.Filter.Eq("Username", userPflist.Username),
Builders<UserPortfolioList>.Update.Push(x => x.Pflist, userPflist.Pflist[0]));
注意:.Push()
的 lambda 表达式在 值是对象 而不是数组时有效。
将多个元素压入数组
要将多个元素压入数组,您需要一个 $each
运算符。
MongoDB查询:
db.collection.update({
username: "aaa"
},
{
$push: {
"pflist": {
$each: [
{
"pfName": "US TECH C",
"symbols": [
"AAPL",
"TSLA"
]
}
]
}
}
})
MongoDB C#:
await _UserPortfoliosCollection.UpdateOneAsync(
Builders<UserPortfolioList>.Filter.Eq("Username", userPflist.Username),
Builders<UserPortfolioList>.Update.PushEach(x => x.Pflist, userPflist.Pflist));
Update Result