在mongodb中的subDocument数组上一起使用$ slice和$ regex [英] Using $slice with $regex together on subDocument array in mongodb
问题描述
我在mongo中遇到了问题.首先让我向您展示一个数据示例.这是我桌子上的记录.
Hi I am stuck on a problem in mongo. Let me first show you an example of the data. This is a record from my table.
{
"_id": "3691149613248",
"following": [{
"content_id": "2584833593728",
"date": "2015-08-20 12:46:55"
}, {
"content_id": "3693447751360",
"date": "2015-09-11 12:17:55"
}, {
"content_id": "2582396936896",
"date": "2015-09-12 07:04:02"
}, {
"content_id": "3697346507456",
"date": "2015-09-14 09:56:03"
}, {
"content_id": "3697755500800",
"date": "2015-09-16 10:05:51"
}, {
"content_id": "2589701320192",
"date": "2015-09-16 10:51:19"
}, {
"content_id": "2585723555136",
"date": "2015-09-16 11:40:26"
}, {
"content_id": "3695996668352",
"date": "2015-09-16 12:50:25"
}, {
"content_id": "3694290368512",
"date": "2015-09-16 12:50:33"
}, {
"content_id": "3691210127552",
"date": "2015-09-16 13:02:57"
}, {
"content_id": "3694134958464",
"date": "2015-09-16 13:06:17"
}, {
"content_id": "3697315148736",
"date": "2015-09-17 06:58:35"
}, {
"content_id": "3692104837824",
"date": "2015-09-17 12:19:12"
}, {
"content_id": "3693400309376",
"date": "2015-09-22 05:43:04"
}]
}
我想获取following
数组,条件是仅特定记录要获取,即以限制和偏移量指定的前缀369
和content_id
的获取编号的content_ids
.
I want to fetch following
array with condition that only specific records to fetch i.e. content_ids
with prefix 369
and fetch number of content_id
specified in limit and offset.
我正在使用$slice
来获取给定限制& following
数组的偏移量.但是如何过滤content_id
和$slice
.
I am using $slice
for fetching records for given limit & offset for following
array. But how to filter content_id
along with $slice
.
我当前的查询
db.collectionName.find({
_id: "3691149613248"
}, {
"following": {
"$slice": [0, 10]
}
});
这是使用限制&中指定的content_id
提取following
数组.抵消.但是,它正在提取所有content_id
,包括前缀258
& 369
,但我只需要使用mongo查询使用带前缀369
的content_id
.
This is fetching following
array with content_id
that is specified in limit & offset. But it is fetching all content_id
including prefix 258
& 369
but I only need content_id
with prefix 369
using mongo query.
任何人都可以帮忙吗?
推荐答案
您可以使用 $ unwind 和 $ match 与mongo 聚合以获得预期的输出,例如:
You can use combination of $unwind and $match with mongo aggregation to get expected output like:
db.collection.aggregate({
$match: {
"_id": "3691149613248" // you can skip this condition if not required
}
}, {
$unwind: "$following"
}, {
$match: {
"following.content_id": {
$regex: /^369/
}
}
}, {
$group: {
_id: "$_id",
"following": {
$push: "$following"
}
}
})
如果您要应用跳过和限制到上面的查询中,那么您可以像这样轻松地使用它:
If you want to apply skip and limit to above query then you can easily use it like:
db.collection.aggregate({
$match: {
"_id": "3691149613248" //use this if you want to filter out by _id
}
}, {
$unwind: "$following"
}, {
$match: {
"following.content_id": {
$regex: /^369/
}
}
}, {
$skip: 4 // you can set offset here
}, {
$limit: 3 // you can set limit here
}, {
$group: {
_id: "$_id",
"following": {
$push: "$following"
}
}
})
编辑:
如果您使用的PHP版本低于5.4,则查询将为:
If you are using php version less than 5.4 then query will be as:
$search = "369";
$criteria = array(array("$match" => array("_id" => "3691149613248")),
array("$unwind" => "$following"),
array("$match" => array("following.content_id" => array("$regex" => new MongoRegex("/^$search/")))),
array("$skip" => 4), array("$limit" => 3),
array("$group" => array("_id" => "$_id", "following" => array("$push" => "$following"))));
$collection - > aggregate($criteria);
如果您使用的PHP版本大于5.3,则分别用[
和]
替换{
和}
括号.
If you are using PHP version greater than 5.3 then just replace {
and }
braces with [
and ]
respectively.
这篇关于在mongodb中的subDocument数组上一起使用$ slice和$ regex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!