django pymongo搜索,排序,限制内部数组并计数它们

发布于 2025-01-23 02:29:12 字数 1273 浏览 2 评论 0原文

我正在与MongoDB一起学习Django,并有一个可以搜索的内容。这是集合中的示例文档:

{ 
    "_id": {    "$oid": "62615907568ddfca4fef3a25"  }, 
    "word": 'entropy, 
    "count": 4,
    "occurrence": [
        {"book": "62615907568ddfca4fef3a23",  "year": 1942, "sentence": 0 },
        {"book": "62615907568ddfca4fef3a23",  "year": 1942, "sentence": 5 },
        {"book": "62615907568ddfca4fef3a75",  "year": 1928, "sentence": 0 },
        {"book": "62615907568ddfca4fef3a90",  "year": 1959, "sentence": 8 } 
    ]
}

我想检索特定文档(word)的“发生”字段的数组元素(word):

  1. 年度
  2. 限制限制的年度和偏移
  3. 无限制/偏移)

计数的总结果( 到目前为止,我已经完成了:

offset= 0
limit= 10
search= {'$and': [{"_id": word_id_obj, "occurrence": {"$elemMatch": {"year": {"$gte": 1940, "$lte": 1960}}}}]}
word_docs= wordcollec.aggregate([
{"$match": search},
{"$project":
    {"occurrence":
        {"$slice": ['$occurrence', offset, limit]}
    }
},
{"$sort": {'occurrence.year': -1}}
])

# Count total results
recnt= wordcollec.aggregate([{"$match": search}, {'$group' : {"_id": "$_id", "sno" : {"$sum" : {"$size": "$occurrence"}}}}])

年度范围,计数无法正常工作,我无法对它们进行分类。如何为此重写我的查询?

提前致谢。

I am learning Django with MongoDb and have a collection to search into. Here is a sample document in the collection:

{ 
    "_id": {    "$oid": "62615907568ddfca4fef3a25"  }, 
    "word": 'entropy, 
    "count": 4,
    "occurrence": [
        {"book": "62615907568ddfca4fef3a23",  "year": 1942, "sentence": 0 },
        {"book": "62615907568ddfca4fef3a23",  "year": 1942, "sentence": 5 },
        {"book": "62615907568ddfca4fef3a75",  "year": 1928, "sentence": 0 },
        {"book": "62615907568ddfca4fef3a90",  "year": 1959, "sentence": 8 } 
    ]
}

I want to retrieve the array elements of 'occurrence' field of a specific document (word):

  1. Sorted by year
  2. Within an year range
  3. Limited with offset
  4. count the total results (without limit/offset)

What I have done so far is:

offset= 0
limit= 10
search= {'$and': [{"_id": word_id_obj, "occurrence": {"$elemMatch": {"year": {"$gte": 1940, "$lte": 1960}}}}]}
word_docs= wordcollec.aggregate([
{"$match": search},
{"$project":
    {"occurrence":
        {"$slice": ['$occurrence', offset, limit]}
    }
},
{"$sort": {'occurrence.year': -1}}
])

# Count total results
recnt= wordcollec.aggregate([{"$match": search}, {'$group' : {"_id": "$_id", "sno" : {"$sum" : {"$size": "$occurrence"}}}}])

The year range, count are not working and I am unable to sort them. How can I rewrite my query for this?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離人涙 2025-01-30 02:29:12

使用$ utind然后$ sort

db.collection.aggregate([
  {
    $match: {
      _id: { "$oid": "62615907568ddfca4fef3a25" },
      occurrence: { $elemMatch: { year: { $gte: 1940, $lte: 1960 } } }
    }
  },
  {
    $set: { totalWithoutLimitOrOffset: { $size: "$occurrence" } }
  },
  {
    $unwind: "$occurrence"
  },
  {
    $match: { "occurrence.year": { $gte: 1940, $lte: 1960 } }
  },
  {
    $sort: { "occurrence.year": -1 }
  },
  {
    $skip: 1
  },
  {
    $limit: 2
  },
  {
    $group: {
      _id: "$_id",
      word: { $first: "$word" },
      count: { $first: "$count" },
      totalWithoutLimitOrOffset: { $first: "$totalWithoutLimitOrOffset" },
      occurrence: { $push: "$occurrence" }
    }
  }
])

mongoplay

Use $unwind then $sort

db.collection.aggregate([
  {
    $match: {
      _id: { "$oid": "62615907568ddfca4fef3a25" },
      occurrence: { $elemMatch: { year: { $gte: 1940, $lte: 1960 } } }
    }
  },
  {
    $set: { totalWithoutLimitOrOffset: { $size: "$occurrence" } }
  },
  {
    $unwind: "$occurrence"
  },
  {
    $match: { "occurrence.year": { $gte: 1940, $lte: 1960 } }
  },
  {
    $sort: { "occurrence.year": -1 }
  },
  {
    $skip: 1
  },
  {
    $limit: 2
  },
  {
    $group: {
      _id: "$_id",
      word: { $first: "$word" },
      count: { $first: "$count" },
      totalWithoutLimitOrOffset: { $first: "$totalWithoutLimitOrOffset" },
      occurrence: { $push: "$occurrence" }
    }
  }
])

mongoplayground

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文