2

I'm trying to update a document which has a specific ID with the current date/time but the below code is not resulting in the DB getting updated and no errors. Any help would be great, thanks.

Schema:

var MerchantShema = new Schema({ merchant_id: Number, merchant_aw_id: Number, merchant_name: String, merchant_url: String, merchant_image: String, product_feed: String, product_feed_updated: Date, created_at: {type: Date, default: Date.now}, updated_at: {type: Date, default: Date.now} }); 

Update Query:

updateMerchantLastProductUpdate: function (mID) { now = new Date(); var query = { "merchant_aw_id" : mID }; Merchants.update(query, { "product_feed_updated": now }, function (err) { if (err) return console.error(err); }) } 

Route

 app.get('/queries', function (req, res) { queries.updateMerchantLastProductUpdate("2926"); }); 

Example document

{ "_id": { "$oid": "55997638e4b01f0391cb99aa" }, "merchant_id": "0003", "merchant_aw_id": "2926", "merchant_name": "Multipower", "merchant_url": "www.multipower.com/uk/", "merchant_image": "", "product_feed": "aw", "product_feed_updated": "", "created_at": "", "updated_at": "" } 

2 Answers 2

5

The merchant_aw_id field in your mongoose schema is expecting a number so you need to parse the string for integer by using the parseInt() method in your query. You also need the $set update operator which replaces the value of a field with the specified value to update your document, together with the {multi: true} option which if set to true, updates multiple documents that meet the query criteria. If set to false, updates one document. The default value is false:

updateMerchantLastProductUpdate: function (mID) { var now = new Date(), query = { "merchant_aw_id" : parseInt(mID) }, update = { "$set": { "product_feed_updated": now } }, options = { "multi": true }; Merchants.update(query, update, options, function (err) { if (err) return console.error(err); }) } 
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks but just tried this and I have the same issue. DB not updating/no error
1

My error was caused by my model have the ID I was looking for in the format Number but my data in mongoDB was a String

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.