假设我的收藏中有以下文件:
{
"_id":ObjectId("562e7c594c12942f08fe4192"),
"shapes":[
{
"shape":"square",
"color":"blue"
},
{
"shape":"circle",
"color":"red"
}
]
},
{
"_id":ObjectId("562e7c594c12942f08fe4193"),
"shapes":[
{
"shape":"square",
"color":"black"
},
{
"shape":"circle",
"color":"green"
}
]
}
Run Code Online (Sandbox Code Playgroud)
查询:
db.test.find({"shapes.color": "red"}, {"shapes.color": 1})
Run Code Online (Sandbox Code Playgroud)
要么
db.test.find({shapes: {"$elemMatch": {color: "red"}}}, {"shapes.color": 1})
Run Code Online (Sandbox Code Playgroud)
返回匹配的文档(文档1),但始终包含所有数组项shapes:
{ "shapes":
[
{"shape": "square", "color": "blue"},
{"shape": "circle", "color": "red"}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,我想仅使用包含以下内容的数组来获取文档(文档1)color=red:
{ "shapes":
[
{"shape": "circle", "color": "red"}
]
}
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我试图查询该属性,该属性是对另一个架构的引用和一些其他数据的数组。为了更好的说明,以下是模式:
var orderSchema = new Schema({
orderDate: Date,
articles: [{
article: {
type: Schema.Types.ObjectId,
ref: 'Article'
},
quantity: 'Number'
}]
}),
Order = mongoose.model('Order', orderSchema);
Run Code Online (Sandbox Code Playgroud)
虽然我成功地查询了引用,即:
Order.find({}).populate('articles.article', null, {
price: {
$lte: 500
}
}).exec(function(err, data) {
for (var order of data) {
for (var article of order.articles) {
console.log(article);
}
}
});
Run Code Online (Sandbox Code Playgroud)
我在查询quantity属性时遇到了一些问题,即这不起作用:
Order.find({}).where({
'articles.quantity': {
$gte: 5
}
}).populate('articles.article', null, {
/*price: {
$lte: 500
}*/
}).exec(function(err, data) {
for (var order of data) {
for …Run Code Online (Sandbox Code Playgroud) mongoose mongodb node.js mongodb-query aggregation-framework
我知道如何使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
我试图找到(使用正则表达式)一个数组字段并仅返回该元素
这是我的数据
[
{
"_id": "56d6e8bbf7404bd80a017edb",
"name": "document1",
"tags": [
"A area1",
"B area2",
"C area3"
]
},
{
"_id": "56d6e8bbf7404bd82d017ede",
"name": "document2",
"tags": [
"b_area3",
"b_area4",
"b_area5"
]
}
]
Run Code Online (Sandbox Code Playgroud)
我的查询
var query=new RegExp('^'+string, "i");
Model.find({tags:query},{'tags.$': 1}, function(err,data){
if(err) console.log(err);
res.json(data);
});
Run Code Online (Sandbox Code Playgroud)
此查询仅选择标记字段(如我所愿),但选择第一个元素.我需要与查询匹配的元素.
编辑:我也尝试了mongodb聚合,$ filter cond是错误的.我收到错误"MongoError:invalid operator $ regex"
caseNote.aggregate([
{ $match: {tags:query}},
{ $project: {
tags: {$filter: {
input: 'tags',
as: 'item',
cond: {$regex: ['$$item', query]}
}}
}}
], function (err, result) {
if (err) {
console.log(err);
} …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Spring查询Mongo存储库并过滤数组子文档。我已经参考过如何使用mongodb过滤子文档中的数组,但是想知道是否有更合适的或Java结构化的方法来使用Spring。
我目前正在使用速记存储库接口符号,但是我正在获取未过滤数组的完整文档。
PersonRepository.java
@Repository
public interface PersonRepository extends MongoRepository <Person, String> {
List<Person> findByAddressZipCode(@Param("zip") int zip);
}
Run Code Online (Sandbox Code Playgroud)
人.java
@Document
public class Person {
@Id
private String id;
private String firstName;
private String lastName;
private List<Address> address;
}
Run Code Online (Sandbox Code Playgroud)
地址.java
public class Address {
private int zip;
}
Run Code Online (Sandbox Code Playgroud)
样本输入
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345"
}]
}
Run Code Online (Sandbox Code Playgroud)
预期产量
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"12345"
}]
}
Run Code Online (Sandbox Code Playgroud)
实际产量
{
"firstName":"George",
"lastName":"Washington",
"address":[{
"zip":"12345"
},{
"zip":"98765"
},{
"zip":"12345" …Run Code Online (Sandbox Code Playgroud) 我正在使用此问题中的方法如何使用MongoDB过滤子文档中的数组
它按预期工作,除非数组中的所有元素都不匹配测试.在这种情况下,我只得到一个没有父数据的空数组.
样本数据
{
"_id": "53712c7238b8d900008ef71c",
"dealerName": "TestDealer",
"email": "test@test.com",
"address": {..},
"inventories": [
{
"title": "active",
"vehicles": [
{
"_id": "53712fa138b8d900008ef720",
"createdAt": "2014-05-12T20:08:00.000Z",
"tags": [
"vehicle"
],
"opts": {...},
"listed": false,
"disclosures": {...},
"details": {...}
},
{
"_id": "53712fa138b8d900008ef720",
"createdAt": "2014-05-12T20:08:00.000Z",
"tags": [...],
"opts": {...},
"listed": true,
"disclosures": {...},
"details": {...}
}
]
},
{
"title": "sold",
"vehicles": []
}
]
}
Run Code Online (Sandbox Code Playgroud)
想要做
在我的查询中,我想返回用户(文档)顶级信息(dealerName,电子邮件)和一个名为vehicle的属性,其中包含列出属性设置为的"活动"库存中的所有车辆true.
我怎么了
这是我的查询.(我使用Mongoose但主要使用本机Mongo功能)
{
$match:
email: params.username
}
{
$unwind: '$inventories'
}
{ …Run Code Online (Sandbox Code Playgroud) 我有一个文档嵌套数组的集合,我只想选择下面的数组,有可能吗?
我已经尝试过了,但是没有用:
db.collection.find({},{'family.children.$.toys' :1})
Run Code Online (Sandbox Code Playgroud)
文件范例
{
"id":1000,
"name": "Bob",
"surname":"The Builder",
"family":{
"size":2,
"status": "happy",
"children":[{
"name":"Jim",
"school": "St. Mary",
"toys":[{
"name":"Lego"
},
{
"name":"Playstation"
}]
},
{
"name":"Kate",
"school": "St. Mary",
"toys":[{
"name":"Xbox"
},
{
"name":"Barbie"
}]
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
预期结果(仅提取玩具清单):
{
_id:1000,
family:{
childrens:[{
toys:[{
name:Lego
},
{
name:Playstation
}]
},
{
toys:[{
name:Xbox,
},
{
name:Barbie
}]
}
]
}}
Run Code Online (Sandbox Code Playgroud)