如何查询数组并从 MongoDB 中检索它
How to query an array and retrieve it from MongoDB
已更新:
我在数据库中有一个如下所示的文档:
我的问题如下:
如何从数据库中检索 friendsArray
的前 10 个元素,并根据 lastTimestamp
值对其进行降序或升序排序。
我不想将所有值下载到我的 API,然后在 Python 中对它们进行排序,因为那会浪费我的资源。
我已经尝试使用此代码 (Python):
listOfUsers = db.user_relations.find_one({'userId': '123'}, {'friendsArray' : {'$orderBy': {'lastTimestamp': 1}}}).limit(10)
但它只是给我这个错误 pymongo.errors.OperationFailure: Unknown expression $orderBy
此时的任何答案都将非常有帮助!谢谢!
使用聚合
首先 unwind
然后 sort
根据时间间隔
group
按_id 创建排序数组
使用 addfields
和 filter
获取数组
的前 10 项
db.collection.aggregate([
{ $match:{userId:"123"}},
{
"$unwind": "$friendsArray"
},
{
$sort: {
"friendsArray.lastTimeStamp": 1
}
},
{
$group: {
_id: "$_id",
friendsArray: {
$push: "$friendsArray"
}
},
},
{
$addFields: {
friendsArray: {
$filter: {
input: "$friendsArray",
as: "z",
cond: {
$lt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
10
]
}// 10 is n first item
}
}
},
}
])
https://mongoplayground.net/p/2Usk5sRY2L2
对于分页使用这个
db.collection.aggregate([
{ $match:{userId:"123"}},
{
"$unwind": "$friendsArray"
},
{
$sort: {
"friendsArray.lastTimeStamp": 1
}
},
{
$group: {
_id: "$_id",
friendsArray: {
$push: "$friendsArray"
}
},
},
{
$addFields: {
friendsArray: {
$filter: {
input: "$friendsArray",
as: "z",
cond: {
$and: [
{
$gt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
10
]
},
{
$lt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
20
]
},
]
}// 10 is n first item
}
}
},
}
])
将您的查找转换为聚合(我们需要说明为什么使用聚合)就像下面的查询。
查询(降序用 -1 替换 1)
db.collection.aggregate([
{
"$match": {
"userId": "123"
}
},
{
"$unwind": {
"path": "$friendsArray"
}
},
{
"$sort": {
"friendsArray.lastTimeStamp": 1
}
},
{
"$limit": 10
},
{
"$replaceRoot": {
"newRoot": "$friendsArray"
}
}
])
如果你想在 limit 之前跳过一些也添加一个阶段
{
"$skip" : 10
}
以10-20条消息为例
已更新:
我在数据库中有一个如下所示的文档:
我的问题如下:
如何从数据库中检索 friendsArray
的前 10 个元素,并根据 lastTimestamp
值对其进行降序或升序排序。
我不想将所有值下载到我的 API,然后在 Python 中对它们进行排序,因为那会浪费我的资源。
我已经尝试使用此代码 (Python):
listOfUsers = db.user_relations.find_one({'userId': '123'}, {'friendsArray' : {'$orderBy': {'lastTimestamp': 1}}}).limit(10)
但它只是给我这个错误 pymongo.errors.OperationFailure: Unknown expression $orderBy
此时的任何答案都将非常有帮助!谢谢!
使用聚合
首先 unwind
然后 sort
根据时间间隔
group
按_id 创建排序数组
使用 addfields
和 filter
获取数组
db.collection.aggregate([
{ $match:{userId:"123"}},
{
"$unwind": "$friendsArray"
},
{
$sort: {
"friendsArray.lastTimeStamp": 1
}
},
{
$group: {
_id: "$_id",
friendsArray: {
$push: "$friendsArray"
}
},
},
{
$addFields: {
friendsArray: {
$filter: {
input: "$friendsArray",
as: "z",
cond: {
$lt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
10
]
}// 10 is n first item
}
}
},
}
])
https://mongoplayground.net/p/2Usk5sRY2L2
对于分页使用这个
db.collection.aggregate([
{ $match:{userId:"123"}},
{
"$unwind": "$friendsArray"
},
{
$sort: {
"friendsArray.lastTimeStamp": 1
}
},
{
$group: {
_id: "$_id",
friendsArray: {
$push: "$friendsArray"
}
},
},
{
$addFields: {
friendsArray: {
$filter: {
input: "$friendsArray",
as: "z",
cond: {
$and: [
{
$gt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
10
]
},
{
$lt: [
{
$indexOfArray: [
"$friendsArray",
"$$z"
]
},
20
]
},
]
}// 10 is n first item
}
}
},
}
])
将您的查找转换为聚合(我们需要说明为什么使用聚合)就像下面的查询。
查询(降序用 -1 替换 1)
db.collection.aggregate([
{
"$match": {
"userId": "123"
}
},
{
"$unwind": {
"path": "$friendsArray"
}
},
{
"$sort": {
"friendsArray.lastTimeStamp": 1
}
},
{
"$limit": 10
},
{
"$replaceRoot": {
"newRoot": "$friendsArray"
}
}
])
如果你想在 limit 之前跳过一些也添加一个阶段
{
"$skip" : 10
}
以10-20条消息为例