Giter VIP home page Giter VIP logo

Comments (3)

Irrelon avatar Irrelon commented on July 23, 2024

Hi ya,

So I think that the $elemMatch functionality already exists as ForerunnerDB simply accepts a query against an array field and will scan the sub-documents to ensure they match. If any sub-documents match then the whole parent document is included in the result which kinda makes $elemMatch irrelevant (except for compatibility).

For example if we have a collection with the documents:

[{
    _id: '1',
    arr: [{
        _id: 'jim',
        name: 'Jim'
    }, {
        _id: 'bob',
        name: 'Bob'
    }]
}, {
    _id: '2',
    arr: [{
        _id: 'alice',
        name: 'Alice'
    }, {
        _id: 'simon',
        name: 'Simon'
    }]
}]

Then query it with:

{
    arr: {
        name: 'Jim
    }
}

The result will return the document with _id: '1'.

This is what I understand that $elemMatch already does but please correct me if I am wrong! :)

The roadmap item relates to updating documents rather than querying them and it might be that we don't actually need that functionality as there is already a way to identify sub-documents in updates.

from forerunnerdb.

hydrotik avatar hydrotik commented on July 23, 2024

I think to your point update functions are there easiest way to illustrate and most applicable. So I have the following query in Mongoose:

Project.update({"_id":data.id, "categories": {$elemMatch: {_id:data.categoryid}}},{$set: {
                "categories.$.name" : data.name,
                "categories.$.description" : data.description
}}, function(){
                // next...
});

This lets me target a record (which I will call project) with the matching id that also matches the id of a nested array called categories. If those criteria match, it updates the value for name and description for only that specific matched record under the category array, which is again nested in the project.

So to use your structure above, by doing a:

Project.update({"_id": 1, "arr": {$elemMatch: {_id: "bob"}}},{$set: {
                "arr.$.name" : "Robert"
}}, function(){
                // next...
});

I would get an updated collection of:

[{
    _id: '1',
    arr: [{
        _id: 'jim',
        name: 'Jim'
    }, {
        _id: 'bob',
        name: 'Robert'
    }]
}, {
    _id: '2',
    arr: [{
        _id: 'alice',
        name: 'Alice'
    }, {
        _id: 'simon',
        name: 'Simon'
    }]
}]

Again, I think this works closely with the $set operator which is part of the update process. Can you give me an example of identifying sub documents in updates? Specifically using arrays as sub documents? Maybe I missed something in the documentation. :)

from forerunnerdb.

Irrelon avatar Irrelon commented on July 23, 2024

$elemMatch (projection) has been added to ForerunnerDB for find() calls. Calls to update() still need to have that functionality added specifically with $elemMatch operators, however a I mentioned above you can specify a sub-array document to be updated already.

from forerunnerdb.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.