nedb
nedb copied to clipboard
Doesn't look like you can $push into arrays
I have two sets of arrays inside my document entry
it appears $push behaves like $set when trying to push a single item into multiple arrays
DB.update({ _id: docId }, {$push: {"arrays.one": 1, "arrays.two":2}},{})
Update: it looks like I can't push items into Arrays with only 1 or no entry period.
STARTING ARRAY = []
DB.update({ _id: ID }, { $push: { "activity.Weekly": 0 } }, {});
DB.update({ _id: ID }, { $push: { "activity.Weekly": 1 } }, {});
DB.update({ _id: ID }, { $push: { "activity.Weekly": 2 } }, {});
DB.update({ _id: ID }, { $push: { "activity.Weekly": 4 } }, {});
DB.update({ _id: ID }, { $push: { "activity.Weekly": 2 } }, {});
DB.update({ _id: ID }, { $push: { "activity.Weekly": 3 } }, {});
Expected Results should be [0,1,2,4,2,3]
Actual Results [3]
attached is image, original array had [0]
in first row, [0,1,1]
& [0,1]
respectfully
You want it to return an array, right? What you're doing now is UPDATING the "activity.Weekly". Basically, you're saying "take whatever value is in 'activity.Weekly' and $push(/update) a new value to it." If you want to store multiple int values, you would also have to turn it into a string, since something like "{ "activity.Weekly": 0, 1, 2 }
," would be invalid and throw an error.
I would either, as said, turn it into a string "0,1,2,...," or turn the value into an array: [0,1,2,...]
.
So something like this:
DB.update({ _id: ID }, { $push: { "activity.Weekly": "0,1,2,4,2,3" } }, {});
Or something like this:
DB.update({ _id: ID }, { $push: { "activity.Weekly": [0,1,2,4,2,3] } }, {});
I was trying to push a value into an empty array per the documentation.
// $push inserts new elements at the end of the array
db.update({ _id: 'id6' }, { $push: { fruits: 'banana' } }, {}, function () {
// Now the fruits array is ['apple', 'orange', 'pear', 'banana']
});
I encountered a bug where arrays with only 1 or less values simply do not work All arrays have to be padded with a 2 item first
This code snippet was my work around, by forcing the document to have a min of 2 entries, else it would result in only the last entry being recorded.
if (docs[entry].activity.Weekly.length <= 1) {
Database.update("Members", { _id: docs[entry]._id }, { $push: { "activity.Weekly": { $each: [0, 0], $slice: -7 } } }, {});
Database.update("Members", { _id: docs[entry]._id }, { $push: { "activity.Monthly": { $each: [0, 0], $slice: -30 } } }, {});
}
so the following would work
Database.update("Members", { _id: docs[entry]._id }, { $push: { "activity.Weekly": { $each: [value], $slice: -7 } } }, {});
Database.update("Members", { _id: docs[entry]._id }, { $push: { "activity.Monthly": { $each: [value], $slice: -30 } } }, {});