如何选择嵌入文档列表作为根元素



我使用MongoDB与mongocsharpdriver。下面是我的数据示例。我想找到所有"类别"中所有"交易"的列表。如何获得使用集合的所有事务的列表。找到方法?查询是否需要在根级别执行"事务"?

这是我的文档:

/* 0 */
{
    "_id" : ObjectId("53791916a33e851d2c7ec8f0"),
    "name" : "Shopping",
    "transactions" : [ 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }, 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }
    ]
}
/* 1 */
{
    "_id" : ObjectId("53791a63a33e851d2c359290"),
    "name" : "Entertainment",
    "transactions" : [ 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }, 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }
    ]
}

在robomongo中我使用:db.Categories。查找({},{_id:0, transactions:1})

,得到:

/* 0 */
{
    "transactions" : [ 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }, 
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }
    ]
}
/* 1 */
{
    "transactions" : [ 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }, 
        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }
    ]
}

这不是我想要的,查询应该返回:

        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
            "date" : "12/01/2012",
            "description" : "mid week",
            "amount" : 12
        }
        {
            "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
            "date" : "12/01/2012",
            "description" : "end of week",
            "amount" : 5
        }
        {
            "_id" : ObjectId("53791a63a33e851d2c35928e"),
            "date" : "12/01/2012",
            "description" : "games",
            "amount" : 70
        }
        {
            "_id" : ObjectId("53791a63a33e851d2c35928f"),
            "date" : "12/01/2012",
            "description" : "films",
            "amount" : 20
        }

每当您想要重塑输出的结构时,您通常都希望使用aggregate而不是find。在这种情况下,您可以使用$unwind$group操作符来实现您想要的。

在shell中:

db.test.aggregate([
  // Duplicate each doc, once per transactions element.
  {$unwind: '$transactions'},
  // Regroup transactions back together into one big array.
  {$group: {_id: null, transactions: {$push: '$transactions'}}}
  ])
输出:

{
    "result" : [ 
        {
            "_id" : null,
            "transactions" : [ 
                {
                    "_id" : ObjectId("53791916a33e851d2c7ec8ee"),
                    "date" : "12/01/2012",
                    "description" : "mid week",
                    "amount" : 12
                }, 
                {
                    "_id" : ObjectId("53791916a33e851d2c7ec8ef"),
                    "date" : "12/01/2012",
                    "description" : "end of week",
                    "amount" : 5
                }, 
                {
                    "_id" : ObjectId("53791a63a33e851d2c35928e"),
                    "date" : "12/01/2012",
                    "description" : "games",
                    "amount" : 70
                }, 
                {
                    "_id" : ObjectId("53791a63a33e851d2c35928f"),
                    "date" : "12/01/2012",
                    "description" : "films",
                    "amount" : 20
                }
            ]
        }
    ],
    "ok" : 1
}

相关内容

  • 没有找到相关文章

最新更新