在mongodb中的subDocument数组上一起使用$ slice和$ regex

编程入门 行业动态 更新时间:2024-10-25 07:33:43
本文介绍了在mongodb中的subDocument数组上一起使用$ slice和$ regex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在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

本文发布于:2023-11-22 15:15:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617934.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   subDocument   mongodb   slice   regex

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!