Cyrus the Great
Cyrus the Great

Reputation: 5942

MongoDB: find a field exist in document

I have written an script for mongodb at robo 3t. but i want to check my document that has not specific field and then my script run. that is mean if Doamin is exist my script do not run. I see this post

but I do not understand well, what can i do!

db.Events.find().forEach(function (item) {
id = item._id;
businessCode = item.BusinessCode

// check Domain filed exist or not
// if (Domain : { $exists: false }) {  // does not work and I got error

this is my document:

"BusinessCode" : "1-2-4-4-5-6-9",
"ProcessId" : UUID("55657890-4433-3993-7933-119876543299"),
"WorkItemId" : NumberLong(423458),
"Domain" : "1"

in some documents Domain is not exist.

Upvotes: 1

Views: 1498

Answers (1)

Atish
Atish

Reputation: 4435

You can check if Domain does not exists in documents within the query as:

 db.Events.find({"Domain":{$exists:false}}).forEach(function (item) { ...

Upvotes: 2

Related Questions