简体   繁体   中英

Multiple documents update mongodb casbah scala

I have two MongoDB collections

promo collection:

{
    "_id" : ObjectId("5115bedc195dcf55d8740f1e"),
    "curr" : "USD",
    "desc" : "durable bags.",
    "endDt" : "2012-08-29T16:04:34-04:00",
     origPrice" : 1050.99,
    "qtTotal" : 50,
    "qtClaimd" : 30,
}

claimed collection:

{
    "_id" : ObjectId("5117c749195d62a666171968"),
    "proId" : ObjectId("5115bedc195dcf55d8740f1e"),
    "claimT" : ISODate("2013-02-10T16:14:01.921Z")

}

Whenever someone claimed a promo, a new document will be created inside "claimedPro" collection where proId is a (virtual) foreign key to first (promo) collection. Every claim should increment a counter "qtClaimd" in "promo" collection. What's the best way to increment a value in another collection in a transactional fashion? I understand MongoDB doesn't have isolation for multiple docs.

Also, reason why I went with "non-embedded" approach is as follow

promo gets created and published to users then claims will happen in 100s of thousands amounts. I didn't think it was logical to embed claims inside promo collection given the number of writes will happen in a single document ('coz mongo resizes promo collection when size grows due to thousands of claims). Having non embedded approach keeps promo collection unaffected but insert new document in "claims" collection. Later while generating report I'll have to display "promo" details along with "claims" details for that promo. With non-embedded approach I'll have to first query "promo" collection and then "claims" collection with "proId". * Also worth mentioning that there could be times where 100s of "claims" can happen simultaneously for the same "promo" * .

What's the best way to achieve trnsactional effect with these two collections? I am using Scala, Casbah and Salat all with Scala 2.10 version.

db.bar.update({ id: 1 }, { $inc: { 'some.counter': 1 } });

Just look at how to run this with SalatDAO, I'm not a play user so I wouldn't want to give you wrong advise about that. $inc is the Mongo way to increment.

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