执行 mongoexport 命令时遇到以下错误。下面的连接字符串。
MongoDB shell 版本 v4.2.0
操作系统 - Mac OS Catalina
mongoexport --uri="mongodb+srv://m001-student:m001-****@sandbox.*****.mongodb.net/sample_supplies" --collection=sales --out=sales.json
Run Code Online (Sandbox Code Playgroud)
错误:
2021-01-14T20:27:59.584+0000 error parsing command line options: error parsing uri: lookup _mongodb._tcp.sandbox.*****.mongodb.net on 192.#.#.#:#:# no such host <br/>
2021-01-14T20:27:59.585+0000 try 'mongoexport --help' for more information
Run Code Online (Sandbox Code Playgroud)
我也在网络访问选项卡中提供了ACCESS FROM ANYWHERE IP 详细信息。在这种情况下,连接时应该不会出现任何问题。我已经在 MacOS 中安装了 home-brew 并安装了 mongodb 数据库工具,因为我的终端最初无法识别 mongoexport 命令。如果我缺少连接字符串中的任何详细信息,请告诉我。
我创建了一个对 Mongo DB 的请求:
{
$project:
{
difference:
{
$subtract: ['$' + endDate, '$' + startDate]
}
}
},
{
$match:
{
difference:
{
$gte: 0
}
}
},
{
$group:
{
_id: null,
avgTime:
{
$divide:
[
{
$avg: "$difference"
}, 60000 // in minutes
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试着:
difference计算结束日期和开始日期之间的差值,并在该差值> 0时取其值但我得到了错误:累加器$divide是一元运算符
我该如何解决这个请求?
我正在尝试插入、更新 MongoDB 数组中的值。
我的MongoDB版本是4.0.5。
这是我的收藏:
{
'id': 1,
'array': [{
'code': 'a'
}, {
'code': 'b'
}]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试进行一些 upsert 查询以插入/更新到数组中,但直到那时我才找到一个好的解决方案。
我的过滤器是:
'id'(指出正确的文件)'array.code'(指向正确的数组单元)'code': 'c'db.test.update({
'id': 1
}, {
$set: {'array.$[elem].test':'ok'}
}, {
upsert: true,
arrayFilters: [{'elem.code': 'c'}]
}
)
Run Code Online (Sandbox Code Playgroud)
我没有错误,但也没有更新插入。
我想像这样在数组中插入元素:
// Desired result
{
'id': 1,
'array': [{
'code': 'a'
}, {
'code': 'b'
}, {
'code': 'c'
'test': 'ok'
}]
}
Run Code Online (Sandbox Code Playgroud)
db.test.update({
'id': 3
}, {
$set: {'array.$[elem].test':'ok'}
}, {
upsert: …Run Code Online (Sandbox Code Playgroud) 我有一个包含此类对象的集合。
{
materials: {
"m1": {
inventory: [
{
price: 100,
amount: 65
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,库存是层次结构深处的一个数组。我想更新amount它的字段。
我从客户端收到物料 ID ( "m1") 和库存索引 ( 0)。
我必须使用更新管道,因为我正在设置和取消设置本文档中的一些其他字段。
这是我尝试过的:
await products.findOneAndUpdate(filters, [
{
$set: {
"materials.m1.inventory.0.amount": 100,
},
},
]);
Run Code Online (Sandbox Code Playgroud)
但它在第 0 个元素内创建一个名为 0 的新字段,并设置amount该对象内部。所以生成的文档看起来像这样。
{
materials: {
"m1": {
inventory: [
{
0: {
amount: 100
}
price: 100,
amount: 65
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我想要的是这样的:
{
materials: {
"m1": {
inventory: [ …Run Code Online (Sandbox Code Playgroud) 假设我们有以下由聚合管道生成的文档:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"description": "description for item 1",
"item_code": "00001"
},
{
"_id": ObjectId("5a934e000102030405000001"),
"description": "description for item 2",
"item_code": "00002"
},
{
"_id": ObjectId("5a934e000102030405000002"),
"description": "description for item 3",
"item_code": "00003"
},
{
"_id": ObjectId("5a934e000102030405000003"),
"extrafield": "extra field for item 2",
"item_code": "00002"
}
]
Run Code Online (Sandbox Code Playgroud)
如何将相同的文档合并item_code为一个,保留所有属性?想要的结果:
[
{
"description": "description for item 1",
"item_code": "00001"
},
{
"description": "description for item 2",
"extrafield": "extra field for item 2",
"item_code": "00002"
},
{
"description": "description for …Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我有一个帖子架构(如下所示):
const postSchema = new mongoose.Schema({
file: {
type: String,
required: true
},
caption: {
type: String,
maxLength: 2000
},
user: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
},
likeNum: {
type: Number,
default: 0,
min: 0
},
likes: [{
type: mongoose.Schema.Types.ObjectId
}]
})
Run Code Online (Sandbox Code Playgroud)
objectid我想在发送用户请求时从 Like 数组中删除 an 。
路线:
const post = await Post.findOne({_id: req.params.postid})
const user = req.user._id
post.update({}, {$pull: {likes: user}})
post.likeNum--
await post.save()
res.send('Unliked')
Run Code Online (Sandbox Code Playgroud)
然而,objectid当调用路由时, 不会从数组中删除。有人能看出为什么吗?谢谢。
更新:
const user = mongoose.Types.ObjectId(req.user._id)
Run Code Online (Sandbox Code Playgroud)
更新2:
Post.updateOne({_id: req.params.postid}, { …Run Code Online (Sandbox Code Playgroud) 我在根级别的模式中有一个字段,并希望将其添加到数组中与条件匹配的每个对象中。
这是一个示例文档......
{
calls: [
{
"name": "sam",
"status": "scheduled"
},
{
"name": "tom",
"status": "cancelled"
},
{
"name": "bob",
"status": "scheduled"
},
],
"time": 1620095400000.0,
"call_id": "ABCABCABC"
}
Run Code Online (Sandbox Code Playgroud)
所需文件如下:
[
{
"call_id": "ABCABCABC",
"calls": [
{
"call_id": "ABCABCABC",
"name": "sam",
"status": "scheduled"
},
{
"name": "tom",
"status": "cancelled"
},
{
"call_id": "ABCABCABC",
"name": "bob",
"status": "scheduled"
}
],
"time": 1.6200954e+12
}
]
Run Code Online (Sandbox Code Playgroud)
应将其call_id添加到数组中状态为“已计划”的所有对象。是否可以通过 mongo 聚合来做到这一点?我已经尝试过$addFields,但无法达到上述结果。提前致谢!
type EventPrefs struct {
Call bool
Presence bool
Endpoint bool
VoiceMail bool
CallRecording bool
}
Run Code Online (Sandbox Code Playgroud)
目前,该结构类型的大小为 5 个字节,但我想使用位。有没有办法做到这一点?
如果区分大小写,如何使排序功能良好。我们怎样才能使它正确
请建议修复它的最佳方法
db.products.aggregate([
{
"$unwind": "$receipe"
},
{
"$unwind": "$receipe.burger"
},
{
"$sort": {
"receipe.burger.name": 1
}
}
])
Run Code Online (Sandbox Code Playgroud)
https://mongoplayground.net/p/2pnUABI_-先生
在我的示例中,familyburger 应该首先显示,而不是 Paneer Burger。
我想根据其先前的值更新 mongodb 中的值,如果null(保存为字符串)那么我会将其更新为新值,否则我想保持原样。
这是我的代码:
User.updateOne({_id: user._id}, {$set: {
deviceId: {$cond: [{$eq: ['null']}, req.body.deviceId, 'null']}
}}, null, function (err, res){
if (err){
return done(err)
}else{
return done(null, user)
}
})
Run Code Online (Sandbox Code Playgroud)
但我收到以下错误(我相信这表明我的语法不正确):
CastError: Cast to string failed for value "{ '$cond': [ { '$eq': [Array] }, 'MYDEVICEID', 'null' ] }" (type Object) at path "deviceId"
at model.Query.exec (D:\X_APP\XAPP_BACKEND\node_modules\mongoose\lib\query.js:4478:21)
at _update (D:\X_APP\XAPP_BACKEND\node_modules\mongoose\lib\query.js:4330:11)
at model.Query.Query.updateOne (D:\X_APP\XAPP_BACKEND\node_modules\mongoose\lib\query.js:4229:10)
at _update (D:\X_APP\XAPP_BACKEND\node_modules\mongoose\lib\model.js:3834:16)
at Function.updateOne (D:\X_APP\XAPP_BACKEND\node_modules\mongoose\lib\model.js:3770:10)
at file:///D:/X_APP/XAPP_BACKEND/middlewares/userMiddleware.js:24:32
at processTicksAndRejections (internal/process/task_queues.js:95:5)
Run Code Online (Sandbox Code Playgroud)
我搜索并看到许多使用聚合的应用程序,但是是否可以使用我的updateOne猫鼬方法来实现它?如果是,我的申请有什么问题?
mongodb ×8
mongoose ×3
node.js ×2
aggregation ×1
arrays ×1
bit ×1
go ×1
javascript ×1
mongoexport ×1
sorting ×1
struct ×1