查询mongoDB使用pymongo在django中通过嵌套对象键范围获取带有跳过/限制的嵌套阵列

发布于 2025-01-22 20:00:06 字数 1921 浏览 1 评论 0原文

我正在和Pymongo一起学习Django。

我有一个MongoDB系列,其中我在一些书籍中存储一些单词及其一年一度的事件。

这些文档以以下格式存储在mongoDB中:

{
   "_id":{
      "$oid":"625c51eec27c99b793074501"
   },
   "word":"entropy",
   "occurrence":13,
   "year":{
      "1942":[
         {
            "book":{
               "$oid":"625c51eec27c99b7930744f9"
            },
            "number":8,
            "sentence":[
               1,
               288,
               322,
               1237,
               2570,
               2585,
               2617,
               2634
            ]
         }
      ],
      "1947":[
         {
            "book":{
               "$oid":"625c5280c27c99b793077042"
            },
            "number":5,
            "sentence":[
               377,
               2108,
               2771,
               3467,
               3502
            ]
         }
      ]
   }
}

现在,我想以 skip and liman> (以及相应的书ID)获取句子列表> _id 和特定范围。

例如,

  1. 我想获取一个数组,其中每一行都是包含“年”,“书”和“句子”的字典。
  2. 该数组将由 _id Year 范围查询。
  3. skip limage 将应用于句子列表,

这是使用django和pymongo的可能任务吗?如果是,最快的方法是什么?

到目前为止,我已经这样做了:

search= {'$and': [{"_id": word_id_obj, "year.1942": {"$exists": 1}}]}
datalist= []
word_docs= wordcollec.find(search, {'year': 1, '_id': 0}).skip(1).limit(5)
sentlist['recordsFiltered']+= wordcollec.count_documents(search)

for b in word_docs:
    year_data= b['year'][1942]
    for by in year_data:
        i= i+1
        this_word= {'serial': i, 'year': cyear, 'book': str(by['book'])}
        datalist.append(this_word)

但是显然,由于 skip 限制正在应用于root文档对象,因此它没有给出所需的结果。此外,具有固定值,没有范围。

似乎使用“ $ slice”是一种选择。但是我无法弄清楚。

感谢您阅读这篇文章。如果您能散发一些灯光,还有更多。

I am learning Django with pymongo.

I have a MongoDB collection where I am storing some words and their year-wise occurrences in some books.

The documents are stored in MongoDB in the following format:

{
   "_id":{
      "$oid":"625c51eec27c99b793074501"
   },
   "word":"entropy",
   "occurrence":13,
   "year":{
      "1942":[
         {
            "book":{
               "$oid":"625c51eec27c99b7930744f9"
            },
            "number":8,
            "sentence":[
               1,
               288,
               322,
               1237,
               2570,
               2585,
               2617,
               2634
            ]
         }
      ],
      "1947":[
         {
            "book":{
               "$oid":"625c5280c27c99b793077042"
            },
            "number":5,
            "sentence":[
               377,
               2108,
               2771,
               3467,
               3502
            ]
         }
      ]
   }
}

Now I want to get the list of the sentences with skip and limit (and the respective book id) queried by _id and for specific year range.

For example,

  1. I want to fetch an array where each row will be a dictionary containing 'year', 'book' and 'sentence'.
  2. The array will be queried by the _id and year range.
  3. A skip and limit will be applied on the sentence list

Is this a possible task using Django and pymongo? If yes, what is the fastest method?

So far I have done this:

search= {'$and': [{"_id": word_id_obj, "year.1942": {"$exists": 1}}]}
datalist= []
word_docs= wordcollec.find(search, {'year': 1, '_id': 0}).skip(1).limit(5)
sentlist['recordsFiltered']+= wordcollec.count_documents(search)

for b in word_docs:
    year_data= b['year'][1942]
    for by in year_data:
        i= i+1
        this_word= {'serial': i, 'year': cyear, 'book': str(by['book'])}
        datalist.append(this_word)

But obviously, it is not giving the desired result as the skip and limit are being applied to the root document object. Also the the year has a fixed value and no range.

It seems using '$slice' is an option. But I cannot figure it out.

Thanks for reading this far. And a lot more if you can throw some light.

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

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

发布评论

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

评论(1

吲‖鸣 2025-01-29 20:00:06

这是一种方法:

...获取一个数组,每行将是字典
包含“年”,“书”和“句子”。

db.collection.aggregate([
  { "$set": { "designWorkAround": { "$objectToArray": "$year" } } },
  { "$set": {
      "designWorkAround": {
        "$map": {
          "input": "$designWorkAround",
          "as": "yearArray",
          "in": {
            "year": "$yearArray.k",
            "books": {
              "$map": {
                "input": "$yearArray.v",
                "as": "bookArray",
                "in": {
                  "bookId": "$bookArray.book",
                  "number": "$bookArray.number",
                  "sentence": "$bookArray.sentence"
                }
              }
            }
          }
        }
      }
    }
  },
  { "$unwind": "$designWorkAround" },
  { "$unwind": "$designWorkAround.books" },
  { "$project": {
      "_id": 0,
      "year": "$designWorkAround.year",
      "book": "$designWorkAround.books.bookId",
      "sentence": "$designWorkAround.books.sentence"
    }
  }
])

尝试一下 mongoplayground.net.net

我不知道您可能想要的所有数据生成或疑问,但是我可能会重新设计该集合,并且每本书中都有一本文档,其中包含文档中的所有相关字段。这将使查询,索引等变得更简单,更有效。

Here's one way to:

... fetch an array where each row will be a dictionary
containing 'year', 'book' and 'sentence'.

db.collection.aggregate([
  { "$set": { "designWorkAround": { "$objectToArray": "$year" } } },
  { "$set": {
      "designWorkAround": {
        "$map": {
          "input": "$designWorkAround",
          "as": "yearArray",
          "in": {
            "year": "$yearArray.k",
            "books": {
              "$map": {
                "input": "$yearArray.v",
                "as": "bookArray",
                "in": {
                  "bookId": "$bookArray.book",
                  "number": "$bookArray.number",
                  "sentence": "$bookArray.sentence"
                }
              }
            }
          }
        }
      }
    }
  },
  { "$unwind": "$designWorkAround" },
  { "$unwind": "$designWorkAround.books" },
  { "$project": {
      "_id": 0,
      "year": "$designWorkAround.year",
      "book": "$designWorkAround.books.bookId",
      "sentence": "$designWorkAround.books.sentence"
    }
  }
])

Try it on mongoplayground.net.

I don't know all the data generation nor queries that you might want, but I would probably redesign the collection and have something like one document per book with all the relevant fields in the document. This would make querying, indexing, etc., simpler and more efficient.

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