No sure what the issue is here:
db.list.insert({ "list": { "sub": [4] } })
db.list.update(
{},
{ "$push": { "list.sub": { "$each": [1,2,3], "$position": 0 } } }
)
{ "list" : { "sub" : [ 1, 2, 3, 4 ] } }
So that works as expected.
And for the other example:
db.list.insert({
outer: [
{
key: "a",
inner: [4]
},
{
key: "b",
inner: [4]
}
]
})
db.list.update(
{ "outer.key": "b" },
{ "$push": {
"outer.$.inner": {
"$each": [1,2,3], "$position": 0
}
}}
)
Again is as expected:
{
"outer" : [
{
"key" : "a",
"inner" : [
4
]
},
{
"key" : "b",
"inner" : [
1,
2,
3,
4
]
}
]
}
The interaction with specific drivers was already explained so there must be something different in the data, but if so then those statements are not valid.
And so exactly the same using Mongoose:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
mongoose.connect('mongodb://localhost/nodetest');
var listSchema = new Schema({
});
var List = mongoose.model( "List", listSchema );
var Other = mongoose.model( "Other", listSchema );
List.collection.update(
{},
{ "$push": {
"list.sub": {
"$each": [ 1, 2, 3 ],
"$position": 0 }
}
},function(err,NumAffected) {
console.log("done");
}
);
Other.collection.update(
{ "outer.key": "b" },
{ "$push": {
"outer.$.inner": {
"$each": [ 1, 2, 3 ],
"$position": 0
}
}},function(err,NumAffected) {
console.log("done2")
}
);