我有像{dob:"02-23-2000"}数据库中的dob 字段。现在我想在字符串格式的 dob 字段上执行$gte和<e如下所示:
db.panelists.count({"dob":{ '$gte': '08-02-1998', '$lte': '08-02-2003' }});
Run Code Online (Sandbox Code Playgroud)
我总是将计数值设为零。
任何人都可以帮助我以相同的 dob 格式解决此查询。
我需要从 mongodb 中存在的单个文档创建一些图。我只能使用 mongodb 聚合框架(所以例如我不能将文档拉入 python 并在那里使用它)。我正在使用元数据库的查询生成器,因此我在这方面受到限制。
为了做到这一点,我首先使用一些$match查询来识别我需要查看的文档(这些文档是预定义的和静态的)。该$match阶段结束后,我留下了一份具有以下结构的文档(这没问题)。
{
"id": 1,
"locs": {
"a":1,
"b":2,
"c":3
}
}
Run Code Online (Sandbox Code Playgroud)
我需要将此结构更改为如下所示:
[{"a":1}, {"b":2}, {"c":3"}]
或任何其他允许我从结构中创建饼图的形式。
谢谢!
使用聚合管道,我试图将嵌入的文档投影到根级别,而不单独投影每个字段,也不替换根级别。
例如,我想将此集合投影到根级别:
[
{
_id: "1",
city: "NY"
user: [ {
firstName: "John",
lastname: "Peters",
brothers: [
{ _id: "B1",
brotherFirstName: "Karl" }
]
}, {
firstName: "Raul",
lastname: "Other",
brothers: [
{ _id: "B2",
brotherFirstName: "May" }
]
}, {
firstName: "Paul",
lastname: "Ray",
brothers: [
{ _id: "B3",
brotherFirstName: "Bryan" }
]
}
},
{
_id: "2",
city: "NY"
user: [ {
firstName: "Joe",
lastname: "Anders",
brothers: [
{ _id: "B4",
brotherFirstName: "Carla" }
]
}, {
firstName: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用最新版本的 mongoose 插入一组对象,或者如果相应的产品 ID 已经存在则更新。我一生都无法找出正确的使用方法(bulkWrite、updateMany 等),而且我似乎无法在不出错的情况下弄清楚语法。例如,我正在尝试
Product.update({}, products, { upsert : true, multi : true }, (err, docs) => console.log(docs))
Run Code Online (Sandbox Code Playgroud)
这会引发错误 DeprecationWarning: collection.update is deprecated. Use updateOne, updateMany, or bulkWrite instead. MongoError: '$set' is empty. You must specify a field like so: {$set: {<field>: ...}
使用 updateMany 只会给我 $set 错误。我似乎找不到任何使用这种方法的人的例子,只是想知道是否有人可以为我提供一个如何正确使用它的例子。
明确地说,我生成了一个对象数组,然后我想将它们插入到我的数据库中,或者更新条目(如果它已经存在)。我要匹配的字段称为pid. 任何提示或建议将不胜感激!
编辑:
我的产品架构
const productSchema = new mongoose.Schema({
title : String,
image : String,
price_was : Number,
price_current : {
dollars : String,
cents : String
}, …Run Code Online (Sandbox Code Playgroud) BookingDetails我有以下记录的收藏:
{
"code" : "TICKET1234",
"orderDetails" : [
{
"cost": 150.0,
"movieName": "avengers"
}
]
},
{
"code" : "TICKET1235",
"orderDetails" : [
]
}
Run Code Online (Sandbox Code Playgroud)
需要检查orderDetails.MovieName投影层上是否存在。我尝试了下面的查询,它没有帮助。
db.BookingDetails.aggregate([
{
$project: {
OrderExists: {
$cond: [
{ $ne: ["$orderDetails.0.movieName", null] },
1, 0
]
}
}
}
])
Run Code Online (Sandbox Code Playgroud)
我无法$exists在 $cond 内部使用。我$ifNull也尝试过。需要您对此的想法。
我同时也是aws和mongodb的新手,所以我在一个非常基本的方面试图连接到我的mongo数据库,托管在亚马逊linux ec2实例上.原因是,我无法构建数据库的路径.
这是我正在尝试使用的:mongoose.connect('mongod://ec2-user@ec2-XX-XX-XXX-XXX-XX.compute-1.amazonaws.com:27017/test' ).
这是我的测试lambda函数的结果: UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): Error: URL malformed, cannot be parsed
我正在使用mongodb 3.6.5.
我正在尝试执行一个棘手的聚合以返回集合中文档内嵌套数组的大小。
以下是重新创建示例数据的方法:
db.test.insert({
projects: [
{
_id: 1,
comments: [
'a',
'b',
'c'
]
},
{
_id: 2,
comments: [
'a',
'b'
]
},
{
_id: 3,
comments: []
}
]
})
Run Code Online (Sandbox Code Playgroud)
我要执行的聚合在这里:
db.test.aggregate([
// enter aggregation here
])
Run Code Online (Sandbox Code Playgroud)
这是所需的输出:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2,
comment_count: 2
},
{
_id: 3,
comment_count: 0
}
]
}]
Run Code Online (Sandbox Code Playgroud)
我正在为如何写这个而苦苦挣扎。如果我尝试以下操作:
"projects.comment_count": {"$size": }
结果返回结果数组的大小:
[{
projects: [
{
_id: 1,
comment_count: 3
},
{
_id: 2, …Run Code Online (Sandbox Code Playgroud) 我是 mongoDb 的新手。我已经设置了两个集合。1) 书 2) 评论
book :
_id
title
author
posted
price
Run Code Online (Sandbox Code Playgroud)
和评论是:
_id
bookId
comment
status
Run Code Online (Sandbox Code Playgroud)
我想得到那些书的评论status = 1。
我试过这个。
return new promise((resolve, reject) => {
db.collection('book').aggregate([
{
$lookup:{
from:'comments',
localField: "_id",
foreignField: "bookId",
as: "comments"
}
}
]).toArray().then((result) => {
if(result.length > 0){
res.send({ status: 1, message: "Success.", data:result });
}else{
res.send({ status: 0, message: "No data found." });
}
}).catch((err) => {
res.send({ status: 0, message: "Something went wrong."});
});
});
Run Code Online (Sandbox Code Playgroud)
当我调用我的 API 时,我在邮递员那里得到了这个。 …
我有一个查询,使用$ lookup “联接”两个模型,此后,我使用$ project选择所有需要的字段,但是$ project带来了很多对象(user_detail),其中包含我需要的更多数据。我只想要结果的两个字段(scheduleStart和scheduleEnd)。
我的查询:
User.aggregate([{
$match: {
storeKey: req.body.store,
}
},
{
$group: {
_id: {
id: "$_id",
name: "$name",
cpf: "$cpf",
phone: "$phone",
email: "$email",
birthday: "$birthday",
lastName: "$lastname"
},
totalServices: {
$sum: "$services"
},
}
},
{
$lookup: {
from: "schedules",
localField: "_id.phone",
foreignField: "customer.phone",
as: "user_detail"
}
},
{
$project: {
_id: 1,
name: 1,
name: 1,
cpf: 1,
phone: 1,
email: 1,
birthday: 1,
totalServices: …Run Code Online (Sandbox Code Playgroud) 这是我的收藏
{
"_id" : ObjectId("5c225f9a66d39d55c036fa66"),
"name" : "Sherlock",
"mobile" : "999999",
"adress" : [
{
"street" : "221b baker street",
"city" : "london"
},
{
"street" : "ben street",
"city" : "london"
}
],
"tags" : [
"Detective",
"Magician",
"Avenger"
]
}Run Code Online (Sandbox Code Playgroud)
现在我想获取地址数组中的第一个或第二个值。为此我正在使用这个命令。
> db.agents.findOne({"name" : "Sherlock"},{"adress" : 1})Run Code Online (Sandbox Code Playgroud)
但它不是给出单个结果,而是给出整个数组,例如
{
"_id" : ObjectId("5c225f9a66d39d55c036fa66"),
"adress" : [
{
"street" : "221b baker street",
"city" : "london"
},
{
"street" : "ben street",
"city" : "london"
}
]
}Run Code Online (Sandbox Code Playgroud)
可以通过比较数组值来完成,例如
db.agents.find({"adress.street": …Run Code Online (Sandbox Code Playgroud)mongodb ×10
mongoose ×3
javascript ×2
node.js ×2
amazon-ec2 ×1
arrays ×1
database ×1
lookup ×1
metabase ×1