I use MongoDB 2.6.2. I need to check in forEach
if field name
exists on selected document.
db.testData.find(...).forEach(function(x){
// insert code here
})
How to check inside forEach
function if x
contains field name
or not ?
Mongo shell is a JavaScript shell, so most of the standard JavaScript methods are supported. You can check if the property exists as you would do on any regular object in JavaScript - by using hasOwnProperty() method:
db.testData.find({}).forEach(function(x){
if (x.hasOwnProperty('name')) {
// Do something
}
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With