我在mongoose中有一个Person对象,那个person对象有多个东西(每个东西都有一个唯一的ID).
person1 = {
things[{id: 1, name: 'one'},{id:2, name: 'two'}]
}
person2 = {
things[{id: 3, name: 'three'},{id:4, name: 'four'}]
}
Run Code Online (Sandbox Code Playgroud)
然后查询:
Person.findOne({'things.id': 2},{'things.$': 1}, function(err, person) { ...
Run Code Online (Sandbox Code Playgroud)
这很好但我正在搜索所有Person对象(可能有很多).在这种情况下,我知道我需要的人的ID和一个'东西'的唯一ID.通过id获取Person可能要快得多:
Person.findById(personId, function(err, person) { ...
Run Code Online (Sandbox Code Playgroud)
然后循环遍历所有事情以找到正确的:
var thing
person.things.forEach(function(t) {
if (t.id == thingId) {
thing = t;
}
});
Run Code Online (Sandbox Code Playgroud)
我想知道的是,如果有更好的方法.我可以通过id查询Person集合来获取一个Person然后过滤掉我正在寻找的东西(没有丑陋的循环)?
如何从嵌套数组中提取数据?
我想提取数组项"值",其中wind_speed参数值介于vitRange.min和vitRange.max之间(twaRange和wind direction的条件相同)
数据:
{
"name" : "race"
,"polaire" : [
{
"voile" : "foc"
, "matrice" :[
{
"vitRange" : { "min" : 0, "max" : 4}
,"twaRange" : { "min" : 0, "max" : 30}
,"values" : [0, 0, 0, 2.4]
},
{
"vitRange" : { "min" : 4, "max" : 6}
,"twaRange" : { "min" : 30, "max" : 33}
,"values" : [0, 0, 2.4, 3.7]
}
]
},
{
"voile" : "spi"
, "matrice" :[
{ …
Run Code Online (Sandbox Code Playgroud) mongodb aggregation-framework spring-data-mongodb spring-boot
我知道如何使MongoDB基于这样的数组找到一行:
useritems.find({userid: useridhere, "items.id": idhere})
Run Code Online (Sandbox Code Playgroud)
但是,我将如何搜索并获取所有激活的项目,或者根据items
属性获取所有项目?例如:
useritems.find({userid: useridhere, "items.activated": true})
Run Code Online (Sandbox Code Playgroud)
将导致从激活的用户获取所有项目是真的.
这是我的项目架构:
var userItemsSchema = new Schema({
userid : String,
items: [
{
id: {type: Number, default: 1},
activated: { type: Boolean, default: false},
endtime : {type: Number, default: 0},
},
],
});
module.exports = mongoose.model('useritems', userItemsSchema);
Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
我在 dotnetcore 2.1 中使用 MongoDB.Driver nuget 包。我试图返回集合中的文档列表,其中子文档字段等于我拥有的列表中包含的任何项目。理想情况下,我需要在 C# 语法中为 dotnetcore 2.1 的 MongoDB.Driver nuget 包提供这个。
文档
{
"_id" : "e29628a65e914c1e91b3fd9cbf6f2353",
"Enabled" : true,
"Name" : "Document123",
"DisplayName" : "Some Document",
"Description" : "Some Description",
"Data" : [
"lastname",
"email",
"firstname",
"zipcode"
],
"Items" : [
{
"_id" : "1",
"Name" : "bob"
},
{
"_id" : "2",
"Name" : "smith"
}
]
}
如果这是 SQL,这就是我想要做的:
SELECT *
FROM Document a, Item b
WHERE a.Id = b.DocumentId AND
b.Name IN ('bob', …
是否可以在嵌套文档中查询特定对象?这是一个例子,
Collection : Threads
{
Documents : Messages
{
threadId = 1
messages = [
{
user = amy
date = 01/01/2012
content = hey
},
{
user = bell
date = 01/01/2012
content = hey
},
{
user = bell
date = 01/02/2012
content = whats up
}
]
},
{
threadId = 2
messages = [
{
user = courtney
date = 01/03/2012
content = first!
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
我希望我的查询说{ threadId : 1, 'messages.date' : { …
给定以下 JSON 示例:
{
_id: "55ef729a66d78d24008xxxx",
name: "The right one"
items: [{
_id: "55ef729a66d78d24008xxxx",
return: true
}, {
_id: "55ef729a66d78d24008xxxx",
return: true
}, {
_id: "55ef729a66d78d24008xxxx",
return: false
}]
}
Run Code Online (Sandbox Code Playgroud)
我想编写一个指定的查询,items.return = true
它应该返回:
{
_id: "55ef729a66d78d24008xxxx",
name: "The right one"
items: [// 2 element array]
}
Run Code Online (Sandbox Code Playgroud)
我见过很多人建议使用 $elemMatch,比如这个问题,但正如它所说,这个
只返回第一场比赛
但这只是返回在匹配 items.return 的数组中具有某些值的父文档。所以在上面的例子中,它将返回所有 3 个数组元素。
我想在返回时完全忽略这些数组值。我不想在客户端做,因为我用items _id做一个项目,如果我不需要它,不想浪费这个项目。
这是一个例子
> db.test.insert({ name: 'test', values: [ { check: true }, { check: false } ] })
> db.find({ values.check: true })[0]
Run Code Online (Sandbox Code Playgroud)
所以我得到真假check
:
{
"_id" : ObjectId("50e22046dc278908f3a38a8e"),
"name" : "test",
"values" : [
{
"check" : true
},
{
"check" : false
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想得到这个:
{
"_id" : ObjectId("50e22046dc278908f3a38a8e"),
"name" : "test",
"values" : [
{
"check" : true
}
]
}
Run Code Online (Sandbox Code Playgroud)
有什么过滤器命令吗?
您好,我正在尝试在 MongoDB 中查询一些嵌套文档。我使用猫鼬..我的文件就像
[
{
"_id": "5a2ca2227c42ad67682731d4",
"description": "some descrition",
"photos": [
{
"_id": "5a2ca22b7c42ad67682731d5",
"approved": false,
"text":"good"
},
{
"_id": "5a2ca72b0a1aa173aaae07da",
"approved": true,
"text":"bad"
},
{
"_id": "5a2cabf85a41077a2f87d4e3",
"approved": false,
"text":"bad"
}
]
}
]
Run Code Online (Sandbox Code Playgroud)
我只想在文本属性中找到值为“good”的照片,这是我的查找代码:
ObjectSchema.find({
'photos': {
$elemMatch : {
'text' : 'good'
}
}
},function(err,result){
console.log(result);
});
Run Code Online (Sandbox Code Playgroud)
我只想返回具有与我的查询匹配的值的元素的对象,但是当此列表中的一个对象匹配时,整个照片列表都会附带结果,而不仅仅是与文本匹配的人.. 我如何查询它,并只带上匹配的元素,在这种情况下,只带文本中具有“好”的元素。
我使用 nodejs + 猫鼬
谢谢!
我的架构是这样的
[
{
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd8"),
"blog_id" : ObjectId("56587befdb7224110f007233"),
"comments" : [
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "pradip is bhole baba",
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd9"),
"dt" : ISODate("2015-12-02T15:33:49.578Z")
},
{
"user_id" : ObjectId("562fa014888806820e21e0df"),
"user_full_name" : "Niroj Paudel",
"comment" : "honkog pokhara... he he ha ha",
"_id" : ObjectId("565f1034fd07cbfc1129db0b"),
"dt" : ISODate("2015-12-02T15:37:24.581Z")
}
],
"record_count" : 2,
"__v" : 0
}]
Run Code Online (Sandbox Code Playgroud)
我正在尝试的是基于blog_id和comments._id .....选择特定的注释项数组,但不是只返回特定注释而是返回整个文档.
目前我有以下查询
db.blog_comments..findOne({
"blog_id" : ObjectId("56587befdb7224110f007233"),
"comments._id":ObjectId("565f1034fd07cbfc1129db0b")
})
Run Code Online (Sandbox Code Playgroud)
此查询返回整个文档,即 -
[
{
"_id" : ObjectId("565f0f5d77f0c7bd11bbadd8"), …
Run Code Online (Sandbox Code Playgroud) 我正在尝试从嵌套在 Mongo 对象中的对象获取信息。数据结构如下所示:
Card{
_id;
contributors: [
{
name;
_id;
},
{
name;
_id;
}
]
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试访问“贡献者”数组中的特定“贡献者”。
Card.findOne({_id: cardId, "contributor._id": contributorId},
(err, contributor) => {
if (err) {
console.log(err);
res.status(500);
res.send({status: "error", message: "sass overload"});
return;
}
console.log(contributor);
res.send(contributor);
});
Run Code Online (Sandbox Code Playgroud)