mongodb查询语句怎么合并查询结果?
迷茫
迷茫 2017-05-02 09:22:41
0
1
616

想用一条语句查询三条记录,包括当前记录、上一条、下一条,怎么实现?
只知道当前记录的_id,需要按另外一个时间字段排序取相临的记录。

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(1)
PHPzhong

It’s difficult to use simple sentences, at least my scope doesn’t feel realistic.
I can provide some ideas:

  1. Use db.coll.find({},{"Only display one column of data, or a few columns, the fewer the better. It is not easy to use when the amount of data is large, because sorting operation is required later"})$natual:1 After sorting, assign the value to the cursor, and then use two variables in forEach() to save the current piece of data and the previous piece of data until the matching condition is successful, and then the three pieces of data before and after and the current piece of data can be returned.
    There may be other more awesome internal methods, welcome to share

  2. Another way is to add a column of ordered numbers or characters based on the data that comes out of $natural:1 regularly, and then save it to another new table, and then query the matching data of the new table based on the ID of the new table. Finally, the KEY: value column that was found and added in order was queried again and the data before and after came out.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template