0

I have the following document:

 {
    _id: asdfasdf,
    title: "ParentA",
    children: [
        {
            _id: abcd <-- using new ObjectId() to generate these on creation
            title: "ChildA",
        },
        {
            _id: efgh,
            title: "ChildB"
        }
    ]
}

What I want to do is use findOne but I only want the returned document to contain a single child in its array.

Sudo logic

Categories.findOne({ _id: "asdfasdf" }, { children: _Id: "abcd" });

I want the returned document to look like this:

{
    _id: asdfasdf,
    title: "ParentA",
    children: [
        {
            _id: abcd <-- using new ObjectId() to generate these on creation
            title: "ChildA",
        }
    ]
}

The purpose of this is so I can pass the information into an edit form, and then update that single child object in the array on save.

I'm getting confused as to how to limit the result set.

Thank you very much!

---- Edit ----

After attempting to use the suggested duplicate question as a reference, I'm getting undefined in my results. I really want to use findOne instead of find() as well. On the client, a collection object, even though it contains one item, is treated differently than a single (findOne) object that is returned.

Here is what I've tried.

db.Category.findOne({
        "_id": parentid,
        "children._id": childid
    },{
        "_id": childid,
        "children": {
            "$elemMatch": {
                "_id": childid
            }
        }
    });



db.Category.findOne({
        "_id": parentid            
    },{
        "children": {
            "$elemMatch": {
                "_id": childid
            }
        }
    });

I've tried several more variations like the above.

---- Edit 2 ----

Based on a comment, here is the output of the following query:

db.category.findOne({ "_id" : "9dYgKFczgiRcNouij"});
{
        "title" : "Appliances",
        "active" : true,
        "children" : [
                {
                        "_id" : ObjectId("680d55c6995ef6f0748278c2"),
                        "title" : "Laundry",
                        "active" : true
                                            },
                {
                        "_id" : ObjectId("2b4469c1a4c8e086942a1233"),
                        "title" : "Kitchen"
                        "active" : true
                },
                {
                        "_id" : ObjectId("4f5562ef7668839704c851d6"),
                        "title" : "Other"
                        "active" : true
                }
        ],
        "_id" : "9dYgKFczgiRcNouij"
}

So I think perhaps my problem is how I created the children._id in the array. I used new ObjectId() to generate the _id.

--- Edit 3 ---

db.category.findOne({
    "_id": "9dYgKFczgiRcNouij"            
},{
    "children": {
        "$elemMatch": {
            "_id": ObjectId("4f5562ef7668839704c851d6")
        }
    }
});

This returns ObjectID is not defined.

user1447679
  • 3,076
  • 7
  • 32
  • 69
  • I've tried referencing the suggested duplicate answer post and will update my question with what I found. Still struggling with this. – user1447679 Oct 04 '14 at 20:23
  • I think my question is different due to the _id of the child object factor, and needing to use findOne() instead of find() due to how the application layer works. @JohnnyHK would you mind please helping me figure this out? Thank you. – user1447679 Oct 04 '14 at 20:27
  • It's the same approach whether you use `find` or `findOne`. Your code looks fine now; make sure that `childid` is the same data type as `_id` in the `children` elements. If it's still not working, update your question to show the actual doc you're expecting to find and what `childid` contains. – JohnnyHK Oct 04 '14 at 21:53
  • @JohnnyHK - Please see my latest update. I feel like a moron... Not sure what I'm doing wrong. – user1447679 Oct 04 '14 at 22:03
  • Nothing wrong with using `new ObjectId()` to generate the `_id` values for the elements, but you need to make sure that `childid` is also an `ObjectId` data type. What does it contain? – JohnnyHK Oct 04 '14 at 22:06
  • @JohnnyHK - I logged the variable in console to check. parentid returns just the "idinhere" and childid returns just like it looks in the mongo query: ObjectId("theidinhere"); – user1447679 Oct 04 '14 at 22:21
  • @JohnnyHK - I added another edit specifically using the Id's the way they look in the document. Is my query incorrect? btw, I appreciate your help. – user1447679 Oct 04 '14 at 22:29
  • Your query code is correct. I try it and get the right answer. What's your output against that example at last? – Wizard Oct 05 '14 at 00:13
  • @Wizard Turns out my last attempt was correct as you've discovered. I had a typo in my code that I didn't have when editing my answer here. GAH! – user1447679 Oct 05 '14 at 02:36

0 Answers0