【问题标题】:Find from another find in mongodb从 mongodb 中的另一个查找中查找
【发布时间】:2016-06-10 11:38:03
【问题描述】:

在 Mysql 中,我可以从另一个 Select 中进行 Select。 所以我会问我是否可以在 Mongodb 中做同样的事情。

为了更多解释,我需要用这个集合的history 对象中的最后一个dateTransaction 来检索特定userOwner 的事务。所以如果这个 userOwner 不在最后一个历史记录中,我们应该'不检索此交易。

我一开始使用这个查询:

@Query(value = "{'history':{'$elemMatch':{'userOwner': ?0}}}")

但它会返回所有元素,即使是该 userOwner 不包含最后一个“dateTransaction”的元素。

所以我的目标是查询它为每个事务返回的查询只是最后一个 dateTransaction 与“历史”数组的 userOwner :

.find({},{dateTransaction: {$slice: 1} }).limit(1)

然后从这个做另一个查询。

任何人都有想法或例子。

Tnx

这是我的收藏,名为“piece”:

{
    "_id" : ObjectId("1"),
    "history" : [{
            "userOwner" : "3",
            "dateTransaction" : ISODate("2016-05-30T00:00:00.000+0000"),
        }, {
            "userOwner" : "1",
            "dateTransaction" : ISODate("2016-05-26T00:00:00.000+0000"),
        }, {
            "userOwner" : "2",
            "dateTransaction" : ISODate("2016-05-23T00:00:00.000+0000"),
        }
    ]
}{
    "_id" : ObjectId("2"),
    "transactions" : [{
            "userOwner" : "2",
            "dateTransaction" : ISODate("2016-05-26T00:00:00.000+0000"),
        }, {
            "userOwner" : "3",
            "dateTransaction" : ISODate("2016-05-15T00:00:00.000+0000"),
        }
    ]
}{
    "_id" : ObjectId("3"),
    "transactions" : [{
            "userOwner" : "2",
            "dateTransaction" : ISODate("2016-05-26T00:00:00.000+0000"),
        }, {
            "userOwner" : "1",
            "dateTransaction" : ISODate("2016-05-15T00:00:00.000+0000"),
        }
    ]
}

例如,userOwner 2 的结果应该是:

{
    "_id" : ObjectId("2"),
    "transactions" : [{
            "userOwner" : "2",
            "dateTransaction" : ISODate("2016-05-26T00:00:00.000+0000"),
        }, {
            "userOwner" : "3",
            "dateTransaction" : ISODate("2016-05-15T00:00:00.000+0000"),
        }
    ]
}{
    "_id" : ObjectId("3"),
    "transactions" : [{
            "userOwner" : "2",
            "dateTransaction" : ISODate("2016-05-26T00:00:00.000+0000"),
        }, {
            "userOwner" : "1",
            "dateTransaction" : ISODate("2016-05-15T00:00:00.000+0000"),
        }
    ]

}

【问题讨论】:

  • 您能补充一些详细信息吗?你知道你的头是什么,我们不是:-)
  • 我的问题可以理解吗?我需要帮助。
  • 只有一个或多个集合吗? select from select可以有很多含义吗?
  • 这是一个名为“piece”的集合
  • 起初谢谢...这不是我搜索的内容,而且很长。最后,我使用了 java...我想要的只是一个简单的 mongo 查询,将其放在前面mongorepository 的方法。

标签: mongodb mongodb-query spring-data-mongodb


【解决方案1】:

看起来您的数据存储在一个集合中 - 所以这是一种糟糕的设计,因为它需要更多的开销来处理......

下面的聚合查询查找同一集合以匹配 user:2 的数据

var unwind = {
    $unwind : "$history"
}
var matchUser = {
    $match : {
        "history.userOwner" : "2"
    }
}
var lookUp = {
    $lookup : {
        from : "shm",
        localField : "userOwner",
        foreignField : "userOwner",
        as : "t"
    }
}

var unwindTransactions = {
    $unwind : "$t"
}
var unwindTransactions2 = {
    $unwind : "$t.transactions"
}
var match2 = {
    $match : {
        "t.transactions.userOwner" : "2"
    }
}
var project = {
    $project : {
        _id : 0,
        recordId : "$_id",
        transactionId : "$t._id",
        dateOfTransaction : "$history.dateTransaction",
        userOwner : "$history.userOwner",
    }
}

db.shm.aggregate([unwind,
        matchUser,
        lookUp,
        unwindTransactions,
        unwindTransactions2,
        match2,
        project
    ])

因此我们有两条用户交易记录

{
    "recordId" : ObjectId("575e7e8b852cb76369c9e446"),
    "transactionId" : ObjectId("575e7e8b852cb76369c9e447"),
    "dateOfTransaction" : ISODate("2016-05-23T00:00:00.000Z"),
    "userOwner" : "2"
},{
    "recordId" : ObjectId("575e7e8b852cb76369c9e446"),
    "transactionId" : ObjectId("575e7e8b852cb76369c9e448"),
    "dateOfTransaction" : ISODate("2016-05-23T00:00:00.000Z"),
    "userOwner" : "2"
}

请考虑以当前形式拆分该集合,在处理更复杂的查询时会让您非常头疼

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-20
    • 1970-01-01
    • 2017-06-24
    • 2019-02-20
    • 2016-02-24
    • 1970-01-01
    • 1970-01-01
    • 2021-08-16
    相关资源
    最近更新 更多