0
db.books.find({ "status": "readED", $and: [ { "listingStatus": { $not: { $exists: true } } } ] }).forEach(function(book){ db.getSiblingDB('reading').bookListing.find({"sellerSku": book.sellerSku, "sellerId":book.seller},{"sellerId": 1, "sellerSku": 1}).forEach(function(bookListing){ printjson(bookListing); }); }); 

when i run this, it gives

 { "_id" : ObjectId("582cb4ad5a5a380sadas038dd2f72"), "sellerId" : "e0a82c3d-079asdas0-49asda97-aasdascdf-0c320e3d7022", "sellerSku" : "te227" } { "_id" : ObjectId("582cb4ae5a5a380038dd2f73"), "sellerId" : "e0a82casdsa3d-07asdas90-4997-acdf-0c320e3d7022", "sellerSku" : "TE231" } 

so it means

there are books which is readed and listingstatus is not exist (normally listingstatus must be existed)

in reading db, there is booklisting colletion. Normally what i need to do is:

  • if a book readed and listingstatus is not existed, need to update listingstatus if this books sellersku exists in booklisting collection.

instead of

 printjson(bookListing); 

i need to update

book.update(listingstatu) in for each but is that possible in javascript?

What aboout this

db.getSiblingDB('book').books.update({ "seller": bookListing.sellerId, "sellerSku": bookListing.sellerSku },{ $set: {"listingStatus" : "LISTING_CREATED"}}) 

instead of

printjson(bookListing) 

it wll again call book db.

but to test i made thi instead of

printjson

 db.getSiblingDB('book').books.find({ "seller": bookListing.sellerId, "sellerSku": bookListing.sellerSku },{ $set: {"listingStatus" : "LISTING_CREATED"}}) 

but it doesnot show anyhing

1 Answer 1

1

replace printjson(bookListing) by this:

 // set listingStatus bookListing.listingStatus = "someRandomValue"; // save the document. As a document already exist in the collection // with this _id, it will update it. db.books.save(listingStatus); 

So your code becomes :

 db.books.find({ "status": "readED", $and: [ { "listingStatus": { $not: { $exists: true } } } ] }).forEach(function(book){ db.getSiblingDB('reading').bookListing.find({"sellerSku": book.sellerSku, "sellerId":book.seller},{"sellerId": 1, "sellerSku": 1}).forEach(function(bookListing){ // set listingStatus bookListing.listingStatus = "someRandomValue"; // save the document. As a document already exist in the collection // with this _id, it will update it. db.books.save(listingStatus); }); }); 

This will update only matching documents

Sign up to request clarification or add additional context in comments.

4 Comments

but i need to add this somerandom value for not all collection. The ones who is not exist and whose sellerid = booklisting seller id
and listingstatus belongss to book db. not booklissting.
@mark bookListing in this context is the JSON document returned by the query, not a mongodb collection
but that json has columns of booklitsting collection. how can books colletion can save it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.