Mongodb 将对象值转换为顶级字符串字段
Mongodb convert object values to top level string field
我有以下查询 mongoDB
{
"name": "juan",
"class": {
"name": "person" // is the field of another collection.
}
}
而我想要实现的是以下
{
"name": "juan",
"class": "person"
}
但是我需要大家的支持才能达到上面的效果
我假设你想更新多个文档,那么下面的查询会有所帮助:
db.collection.update(
{ },
[
{
$set: {
'class': '$class.name'
}
}
],
{ multi:true }
)
如果您只需要以您指定的结构返回数据,您可以将投影与查找查询结合使用:
db.collection.find({}, { name: 1, class: '$class.name' });
希望对您有所帮助。
我有以下查询 mongoDB
{
"name": "juan",
"class": {
"name": "person" // is the field of another collection.
}
}
而我想要实现的是以下
{
"name": "juan",
"class": "person"
}
但是我需要大家的支持才能达到上面的效果
我假设你想更新多个文档,那么下面的查询会有所帮助:
db.collection.update(
{ },
[
{
$set: {
'class': '$class.name'
}
}
],
{ multi:true }
)
如果您只需要以您指定的结构返回数据,您可以将投影与查找查询结合使用:
db.collection.find({}, { name: 1, class: '$class.name' });
希望对您有所帮助。