简体   繁体   中英

Find from another find in mongodb

In Mysql I can do a Select from another Select. So I would ask if I can do the same in Mongodb.

For more explanation, I need to retreive the transaction of the a specific userOwner with just the last dateTransaction in the history object of this collection.So if this userOwner isn't in the last history we shoudn't retreive this transaction.

I used at first this query:

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

but it returns all the elements even those where this userOwner isn't with the last "dateTransaction".

So I aim to de a query that it returns for each transaction just the last dateTransaction with userOwner of the "history" array :

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

and after do another query from this one.

Anyone has an idea or examples.

Tnx

This is my Collection called "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"),
        }
    ]
}

As example the result for the userOwner 2 should be :

{
    "_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"),
        }
    ]

}

it looks like your data is stored in one collection - so this is a kind of bad design as it needs more overhead to work with....

below aggregation query which has a lookup to the same collection to match data for 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
    ])

and as a result we have two records of user transactions

{
    "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"
}

Please consider split of that collection as in current form will give you a lot of headache when processing more complex queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM