0

I have this following strtucture

{
  _id: ".....",
  a: {
    a1:".......",
    b: [
      b1: {
        b11: "......",
        b12: "......",
      },
      b2: {
        b21: "......",
        b22: "......",
        d: {},
      },
      c:{
        c1: {
          ......
        },
        d: {

        }
      }
    ]
  }
}

here I want to check if property d exists or not inside b, it may exists in multiple objects inside b, if exists pull the d object from record.

Note: There might be a chance that property d exists multiple times inside b1 and b2, In this case I want to remove it from all objects

I tried like

Coll.find({ 'a.b': { $elemMatch: { 'c': { d: { $exists: true } } } } })

but it is not returning anything although there are record, any help appreciated.

I want to pull that data from record too.

Thanks.

UPDATE

Coll.find({ 'a.b.c.d'{ $exists: true } })

that worked for me but still no idea how to use positional operator to pull the value from record

1 Answer 1

1

Please take a little more time forming your questions to make them easier to answer: your example data has key-values inside a list (invalid), your question mentions $elemMatch which is a list operator, you talk about removing things but never follow up that thought, and then your UPDATE switches gears and implies it is an object hierarchy.

Taking a hint from your UPDATE, I created some valid data - paste this into mongo shell (perhaps in the test database):

db.test.insert({
  a: {
    a1: "a1",
    b: {
      b1: {
        b11: "b11",
        b12: "b12",
      },
      b2: {
        b21: "b21",
        b22: "b22",
        d: {},
      },
      c: {
        c1: {
          c1: "......"
        },
        d: {
          d1: "woo hoo, struck gold"
        }
      }
    }
  }
})

Inside the mongo shell, everything is javascript so "positional operators" are the dot and array subscript operator. A mongo find() returns an array of documents. If you want the the d document (object) from the first doc returned from the query

db.test.find({'a.b.c.d': {$exists: true}})[0].a.b.c.d

produces

{
  "d1": "woo hoo, struck gold"
}

UPDATE: Adding detail in response to comment.

If you want to remove the a.b.c.d sub-document, use $unset

// remove sub-document a.b.c.d
db.test.update({}, {$unset: {'a.b.c.d': ''}});
// look at the document to verify that a.b.c.d is removed
db.test.find();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer, here b is array of objects, my record structure is correct I feel, . Coming to your answer I want to remove object d from record not to get the data, I can read the data, but to pull data from record I can't find exact query
On arrays: try to insert your example into a collection - review the error. On removing: use $unset to remove a single field. Updating my answer to show how.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.