mongodb - 插入中的MongoDB子查询
问题描述
我搜索类似:
insert into B (col)
select id from anotherCollection limit 1
我对此进行了测试,但我怀疑它不起作用:
db.B.insert([
{
"a": "lores ipsum",
"b": [
db.getCollection('anotherCollection').find({},{_id:1}).limit(1)
]
}
]);
有必要使用投影吗?
解决方案
你不能在 mongo db 中做到这一点,但在 javascript 或 python 中是这样的
var a = db.getCollection('anotherCollection').find({},{_id:1}).limit(1)
db.B.insert([
{
"a": "lores ipsum",
"b": a
}
]);
推荐阅读
- python - python selenium 元素已定位但不可点击 - 超时异常
- oauth - 使用客户端、服务器、数据库和 Auth0 的一次性密码身份验证流程
- android - 如何在android中使用openCV拍照?
- angular - Array.push 使所有对象都相同
- excel - 当值不为零时,循环通过列并将值复制到其他工作表vba excel
- android - BubbleWrap CLI 显示错误 The given android-sdk is not correct when bubblewrap build
- javascript - 如何在mongodb中将集合A中的字符串与集合B匹配
- bash - 从文本文件中提取属性值
- google-cloud-storage - 我们可以使用单个 Google Cloud Dataflow 将来自多个 Pubsub(源)的数据写入多个 GCS(接收器)吗?
- wordpress - woocommerce 中的四舍五入价格