MongoDB通过NodeJS的部分帐户

时间:2022-11-15 03:03:55

Upon viewing the Mongo Count docs, it showed the following code :

查看Mongo Count文档后,它显示以下代码:

    // Peform a partial account where b=1
    collection.count({b:1}, function(err, count) {

where it count every document that has b:1. Those documents can look something like this

它计算每个具有b:1的文档。这些文件看起来像这样

{
    a:1,
    b:1,
    c:1
}

How can I find the partial account on a more in-depth level. For example consider the following document. How can I count every document that contains "Name": "LOG-11-05-SH-O1.mp4" ?

如何在更深入的层面上找到部分帐户。例如,请考虑以下文档。如何计算包含“名称”的每个文档:“LOG-11-05-SH-O1.mp4”?

   "display": {
     "0": {
       "Name": "LOG-11-05-SH-O1.mp4",
       "Type": "Startup",
       "Count": "2",
       "Detail": {
         "0": {
           "Start": "2013-01-20,11:32:22",
           "End": "2013-01-20,11:32:30" 
        },
         "1": {
           "Start": "2013-01-20,11:32:22",
           "End": "2013-01-20,11:32:30" 
        } 
      } 
    },
     "1": { ....

I know that "display.0.Name" : "LOG-11-05-SH-O1.mp4" will count everytime "LOG-11-05-SH-O1.mp4" appears under display 0, but sometimes the number may change. For example, next time time "LOG-11-05-SH-O1.mp4" might be under display 1. Thus is there a way to perform a count on only "LOG-11-05-SH-O1.mp4" or perhaps ignore the middle number?

我知道“display.0.Name”:“LOG-11-05-SH-O1.mp4”将在每次显示0下出现“LOG-11-05-SH-O1.mp4”,但有时候数字可能会更改。例如,下次时间“LOG-11-05-SH-O1.mp4”可能会显示1.因此有一种方法只对“LOG-11-05-SH-O1.mp4”执行计数或或许忽略中间数字?

2 个解决方案

#1


1  

See my comments on your question, but I don't see a way to do this without using an array instead of an object for the value of display. If it is an array, this becomes trivial.

请参阅我对您的问题的评论,但是如果不使用数组而不是显示值的对象,我看不到这样做的方法。如果它是一个数组,这就变得微不足道了。

Suppose your data looked like this:

假设您的数据如下所示:

"display": {[
  {
   "Name": "LOG-11-05-SH-O1.mp4",
   "Type": "Startup",
   "Count": "2",
   "Detail": {
     "0": {
       "Start": "2013-01-20,11:32:22",
       "End": "2013-01-20,11:32:30" 
    },
     "1": {
       "Start": "2013-01-20,11:32:22",
       "End": "2013-01-20,11:32:30" 
    } 
  } 
, { ....}
]}

Then you query would just be:

那你查询就是:

db.foos.count({"display": {$elemMatch: { "Name" : "LOG-11-05-SH-O1.mp4"}}})

Here is the relevant MongoDB page on $elemMatch: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/.

以下是$ elemMatch上的相关MongoDB页面:http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/。

If you must keep the object schema, then you can grab all the records and do the count on the app side and not in MongoDB.

如果你必须保留对象模式,那么你可以获取所有记录并在应用程序端而不是在MongoDB中进行计数。

If someone else knows a way to pull this off without the array, I'm happy to receive a downvote and then just remove what would then be an incorrect answer.

如果其他人知道如何在没有阵列的情况下解决此问题,我很高兴收到一个downvote,然后删除那些不正确的答案。

#2


0  

I feel kind of silly not realizing this at first. The answer is quite simple:

起初我觉得有点傻到没有意识到这一点。答案很简单:

"display.Name": "LOG-11-05-SH-O1.mp4"

This will find all entries under display that has that following name.

这将查找显示中具有以下名称的所有条目。

#1


1  

See my comments on your question, but I don't see a way to do this without using an array instead of an object for the value of display. If it is an array, this becomes trivial.

请参阅我对您的问题的评论,但是如果不使用数组而不是显示值的对象,我看不到这样做的方法。如果它是一个数组,这就变得微不足道了。

Suppose your data looked like this:

假设您的数据如下所示:

"display": {[
  {
   "Name": "LOG-11-05-SH-O1.mp4",
   "Type": "Startup",
   "Count": "2",
   "Detail": {
     "0": {
       "Start": "2013-01-20,11:32:22",
       "End": "2013-01-20,11:32:30" 
    },
     "1": {
       "Start": "2013-01-20,11:32:22",
       "End": "2013-01-20,11:32:30" 
    } 
  } 
, { ....}
]}

Then you query would just be:

那你查询就是:

db.foos.count({"display": {$elemMatch: { "Name" : "LOG-11-05-SH-O1.mp4"}}})

Here is the relevant MongoDB page on $elemMatch: http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/.

以下是$ elemMatch上的相关MongoDB页面:http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/。

If you must keep the object schema, then you can grab all the records and do the count on the app side and not in MongoDB.

如果你必须保留对象模式,那么你可以获取所有记录并在应用程序端而不是在MongoDB中进行计数。

If someone else knows a way to pull this off without the array, I'm happy to receive a downvote and then just remove what would then be an incorrect answer.

如果其他人知道如何在没有阵列的情况下解决此问题,我很高兴收到一个downvote,然后删除那些不正确的答案。

#2


0  

I feel kind of silly not realizing this at first. The answer is quite simple:

起初我觉得有点傻到没有意识到这一点。答案很简单:

"display.Name": "LOG-11-05-SH-O1.mp4"

This will find all entries under display that has that following name.

这将查找显示中具有以下名称的所有条目。