Code Monkey home page Code Monkey logo

Comments (16)

louischatriot avatar louischatriot commented on July 21, 2024

Definitely. I'll implement support for $push, $pop, $addToSet. I should be able to find the time to do it in the next few days, meanwhile I'll leave this open.

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

It's now done, and included in the latest published version (v0.7.7).

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

See the doc here : https://github.com/louischatriot/nedb#updating-documents

from nedb.

 avatar commented on July 21, 2024

Just a thankyou for a superquick fix :)

from nedb.

 avatar commented on July 21, 2024

and I'm back with a possible bug/issue - relating to empty arrays...

Just using $set to add an element which is [] works but you cannot add anything to that 'array' (the isArray test is failing as it's an Object, apparently?)

I created a $pullall method to help me test this (it's probably something we'll need at some point - along with $pull) and it seems the same problem happens when an array is 'emptied'.

So you can use $push or $addToSet to create and fill and array but I cannot figure any way to deal with 'emptying' it again (which seems a legit thing to do?)

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

You can empty an array using the $pop operator, but of course you have to do it element-by-element so using $set is more appropriate. The behaviour you describe is strange, could you copy paste some code that helps me reproduce it on my machine ?

from nedb.

 avatar commented on July 21, 2024

I posted a reply but it seems to have been eaten by something.

Basically, if you create a document with a property which is an array - you cannot then use $pop etc. on that array

e.g.
db.insert({"key": "mykey", "tags": []})
or even
db.insert({"key": "mykey", "tags": ["a","b","c"]})
You cannot then
db.update({"key": "mykey"},{$addToSet: {"tags": "d"}})
it fails with 'not an array'

If you don't set the property on insert, you CAN then 'create' it using $addToSet or $push and it works AOK - but if you $pop everything it reverts into that state of "not being an array" again

A 'console.log(obj[field])' in the $addToSet function shows it to be an [object] at this point

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

I'll see if I can reproduce this and get back to you if I need a more specific code example.

from nedb.

 avatar commented on July 21, 2024

I should add that I'm converting existing code within a node webkit application so it's hard to pull-out bits of code - I'll go look into creating some standalone node code (but I've not done that before so it might take me a while!)

Also - here's my $pullall function - you might want to add it or something like it (and a $pull would be nice!) :)

/**

  • Remove all elements of an array
    */
    lastStepModifierFunctions.$pullall = function (obj, field, value) {
    if (!util.isArray(obj[field])) { throw "Can't $pullall from non-array values"; }
    obj[field] = [];
    };

from nedb.

 avatar commented on July 21, 2024

Update: I can confirm this problem seems to be specific to node webkit - running my test script in regular (W7-64) node works fine - same script in node webkit fails...

Checking what I've got installed, I'm on node webkit 0.5.1 which is node 0.10.5 - wheras my local node.js install is the latest 0.10.11

So I guess it's something which has changed inbetween those releases and/or something being tampered-with by node webkit?

So I updated to nw 0.6.0 but that's still node 0.10.5 and still throws the same errors :(

So I removed node.js and installed the standalone 0.10.5 and that STILL works - so the problem is in node webkit somewhere...

So I Googled and it seems there may be an issue with using isArray and/or instanceOf Array within nw - and I suspect this may be the source of this issue but I'm not really sure where to start looking...???

from nedb.

 avatar commented on July 21, 2024

and I've found and fixed the problem - in model.js line 116 (the deepcopy function) it says

if (obj instanceof Array) {

which should be

if (util.isArray(obj)) {

and then it all seems to work fine...

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

Good for me if you found the problem :) I thought I had removed all the obj instanceof Array ... I'll correct this tomorrow.

from nedb.

 avatar commented on July 21, 2024

I started work on a $pull and $pullall function - this version of $pull only does straight string matching tho, in the ideal world it would use the full query matching capabilies but it's a BIT beyond my JS skills to see how you'd do that - perhaps, when you have a few mins, you might see your way to adding that?

My code is as follows...

/**
 * Remove selected elements of an array
 */
lastStepModifierFunctions.$pull = function (obj, field, value) {
  if (!util.isArray(obj[field])) { throw "Can't $pull from non-array values"; }
  while (obj[field].indexOf(value) !== -1)
    obj[field].splice(obj[field].indexOf(value),1);
};

/**
 * Remove all elements of an array
 */
lastStepModifierFunctions.$pullall = function (obj, field, value) {
  if (!util.isArray(obj[field])) { throw "Can't $pullall from non-array values"; }
  obj[field] = [];
};

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

I just fixed this strange bug and published the new version (v0.7.9), thanks !

Concerning $pull and $pullAll, their effect is a bit different in MongoDB than what your code does: you can actually give a query and MongoDB removes the elements matching this query. See http://docs.mongodb.org/manual/reference/operator/pull/ and http://docs.mongodb.org/manual/reference/operator/pullAll/

I intend to support these operations, but this will be tracked in another issue so I'll close this one. Also, please tell me if you want to do it and submit a PR so that we don't work on the same stuff!

from nedb.

 avatar commented on July 21, 2024

For reference - I wrote those purely to help me resolve the array issues
and I wasn't sure how to integrate your 'query' methods - they're
nice-to-haves but not essential at this point I think

On 21 June 2013 08:59, Louis Chatriot [email protected] wrote:

I just fixed this strange bug and published the new version (v0.7.9),
thanks !

Concerning $pull and $pullAll, their effect is a bit different in MongoDB
than what your code does: you can actually give a query and MongoDB removes
the elements matching this query. See
http://docs.mongodb.org/manual/reference/operator/pull/ and
http://docs.mongodb.org/manual/reference/operator/pullAll/

I intend to support these operations, but this will be tracked in another
issue so I'll close this one. Also, please tell me if you want to do it and
submit a PR so that we don't work on the same stuff!


Reply to this email directly or view it on GitHubhttps://github.com//issues/27#issuecomment-19802911
.

from nedb.

louischatriot avatar louischatriot commented on July 21, 2024

Indeed, but I will still implement them to keep the same API as MongoDB. I should be done within a week or so (lot of other work to do !)

from nedb.

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.