How to $set sub-sub-array items in MongoDB - mongodb

I'm developing a webapp which has a portal-ish component to it (think like multiple panels that can be drug around from column to column and added or removed). I'm using MongoDB to store this info with a format like so...
{
_id: ObjectId(...),
title: 'My Layout',
columns: [
{
order: 1,
width: 30,
panels: [
{ title: 'Panel Title', top: 100, content: '...' },
{ title: 'Panel Title', top: 250, content: '...' },
]
},
{
... multiple columns ...
}
]
}
I'm attempting to use atomic/modifier operations with update() and this is getting confusing. If I wanted to just update one property of a specific panel, how do I reference that?
update(
{ _id: ObjectId(...) },
{ $set: { columns.[???].panels.[???].top: 500 }
)

If you know the index in the array you can access the array element directly using dot notation.
update(
{ _id: ObjectId(xxxx) },
{ $set: { 'columns.0.panels.0.top' : 125}}
)
Make sure you encase the dot notated path in quotes as a string.
Edit:
To give more detail on how this could work dynamically, I'll give an example in PHP:
$action = array("columns.$colNum.panels.$panelNum" => $newValue);
Yes there is the positional operator, but it does not appear to be advanced enough to change arrays within arrays, this may change in MongoDB 1.7.0
There is an alternative you can do instead of trying to stuff this information into a nested document. Try to flatten it out. You can create a collection that has panel & column objects:
column object:
{
_id: // MongoId
type: 'column',
user: 'username',
order: 1,
width: 30,
}
panel object:
{
_id: //MongoId
type: 'panel',
user: 'username',
parentColumn: //the columns _id string
top: 125,
left: 100
}
Then you can find all columns that belong to a user by doing:
find({ type: 'column', user:'username'});
You can find all panels for a specific column by doing:
find({type: 'panel', columnOwner:'ownerID'});
Since each column and panel will have a unique ID given by MongoDB to it, you can easily query and atomically set options.
update({'_id': ObjectId('idstring')}, {$set : { 'top' : 125}});

I too had a situation like this, i followed $addToSet after $pull to replace i.e update what i needed.
update(
{ _id: ObjectId(...) },
{ $addToSet: { columns.$.panels : "new sub-Doc" }
)
And then remove the old value
update({_id:ObjectId(....)},{$pull : { columns.$.panels : {"top" : 100} }})

Related

Mongodb aggregation and conditional push to array

It's been 2 days (or nights should I say) since I am trying to figure out following so would appreciate your help guys.
in mongodb I have number of orders (I will simplify documents for the case).
I want to group all documents by $campRoundId and where there are installments, push all object to installments variable.
The problem I am facing is when document.installments array is empty it pushes it to the array too.
Initial documents (same campRoundId to group by - first with no instalmments, second with 2)
[
{
_id: ObjectId("62792d8a519af6ae8cdff779"),
campRoundId: ObjectId("620a790b2cbc52006c83115a"),
installments: [],
},
{
_id: ObjectId("62792d8a519af6ae8cdff77a"),
campRoundId: ObjectId("620a790b2cbc52006c83115a"),
installments: [
{
payment: 100,
paymentStatus: false,
},
{
payment: 20,
paymentStatus: false,
},
],
},
];
my aggregation
/**
* _id: The id of the group.
* fieldN: The first field name.
*/
{
_id : "$campRoundId",
installments: {
$push: {
$cond:[
{ $gte: ["$installments.length", 1] },
"$installments",
null
]
}
}
}
I want to get rid of empty object, so if there are no installments nothing will be pushed. (dotted lines)

Filtering and measuring mongodb nested array of objects

I have a document of this kind:
{
_id: "123",
arrayName: [
{ text: 'first', field: false},
{ text: 'second', field: true},
{ text: 'third', field: false}
]
}
Now I would like to my query to return the length of filtered arrayName to have only the objects where field === false.
I have tried many queries with db.collection.find({}) but I always receive the entire document and in the end and it count() => to 1.
Any suggestion?
thanks
You can use projection operator to achieve what you want.
Try this:
db.collection.find({
_id : givenId ,
"arrayName.field" : false
},{
"arrayName.$":1
},function(err,result){
if(!err){
len = result.arrayName.length;
//use len however you want.
}
});
"arrayName.$":1 will select only the matched elements of the array. Then you can get the length of array with field:false
Hope this helps!
Another user already give you the right answer for your question using the plain Mongo query. But I think it won't have much because you are using Mongo with Meteor, so here I present you another way to solve your problem by using transform function in find command:
Collection.find({
// ...
}, {
transform(doc) {
doc.filterArrayLength = doc.arrayName.filter(obj => obj.field === false).length;
return doc;
}
}).fetch();
With this command the result will be like:
[
// ...
{
_id: "123",
arrayName: [
{
text: 'first',
field: false
}, {
text: 'second',
field: true
}, {
text: 'third',
field: false
}
],
filterArrayLength: 2
}
]

MongoDB, find field into array

I have items like these in my collection
{
user: data,
somestuff: [somedata ...],
terminals: [ {
label: data,
details: [{more content}, {}, ...]
}]
}
I would use 'find' to extract "details" field for a specific terminal 'label'
I know that there is an easy way to get the "terminals" array by:
collection.find({_id: "..."}, {_id:0, terminals: 1})
Wich return
{ terminals: [ {
label: data,
details: [{more content}, {}, ...]
}]
}
I tried
collection.find({ "terminals.label": data }, { _id: 0, "terminals.$.details": 1 })
As edirican has suggested
And it almost work, but it return the same structure than previously except that the terminals list contain only the labeled document
The result I expect is the details list, extracted from terminals
{ details: [{more content}, {}, ...] }
Thanks for your help !
Use positional ($) projection.
http://docs.mongodb.org/manual/reference/operator/projection/positional/
collection.find({ "terminals.label": 2 }, { _id: 0, "terminals.$.details": 1 })

How can I retrieve all the fields when using $elemMatch?

Consider the following posts collection:
{
_id: 1,
title: "Title1",
category: "Category1",
comments: [
{
title: "CommentTitle1",
likes: 3
},
{
title: "CommentTitle2",
likes: 4
}
]
}
{
_id: 2,
title: "Title2",
category: "Category2",
comments: [
{
title: "CommentTitle3",
likes: 1
},
{
title: "CommentTitle4",
likes: 4
}
]
}
{
_id: 3,
title: "Title3",
category: "Category2",
comments: [
{
title: "CommentTitle5",
likes: 1
},
{
title: "CommentTitle6",
likes: 3
}
]
}
I want to retrieve all the posts, and if one post has a comment with 4 likes I want to retrieve this comment only under the "comments" array. If I do this:
db.posts.find({}, {comments: { $elemMatch: {likes: 4}}})
...I get this (which is exactly what I want):
{
_id: 1,
comments: [
{
title: "CommentTitle2",
likes: 4
}
]
}
{
_id: 2,
comments: [
{
title: "CommentTitle4",
likes: 4
}
]
}
{
_id: 3
}
But how can I retrieve the remaining fields of the documents without having to declare each of them like below? This way if added more fields to the post document, I wouldn't have to change the find query
db.posts.find({}, {title: 1, category: 1, comments: { $elemMatch: {likes: 4}}})
Thanks
--EDIT--
Sorry for the misread of your question. I think you'll find my response to this question here to be what you are looking for. As people have commented, you cannot project this way in a find, but you can use aggregation to do so:
https://stackoverflow.com/a/21687032/2313887
The rest of the answer stands as useful. So I think I'll leave it here
You must specify all of the fields you want or nothing at all when using projection.
You are asking here essentially that once you choose to alter the output of the document and limit how one field is displayed then can I avoid specifying the behavior. The bottom line is thinking of the projection part of a query argument to find just like SQL SELECT.It behaves in that * or all is the default and after that is a list of fields and maybe some manipulation of the fields format. The only difference is for _id which is always there by default unless specified otherwise by excluding it, i.e { _id: 0 }
Alternately if you want to filter the collection you nee to place your $elemMatch in thequery itself. The usage here in projection is to explicitly limit the returned document to only contain the matching elements in the array.
Alter your query:
db.posts.find(
{ comments: { $elemMatch: {likes: 4}}},
{ title: 1, category: 1, "comments.likes.$": 1 }
)
And to get what you want we use the positional $ operator in the projection portion of the find.
See the documentation for the difference between the two usages:
http://docs.mongodb.org/manual/reference/operator/query/elemMatch/
http://docs.mongodb.org/manual/reference/operator/projection/elemMatch/
This question is pretty old, but I just faced the same issue and I didn't want to use the aggregation pipeline as it was simple query and I only needed to get all fields applying an $elemMatch to one field.
I'm using Mongoose (which was not the original question but it's very frequent these days), and to get exactly what the question said (How can I retrieve all the fields when using $elemMatch?) I made this:
const projection = {};
Object.keys(Model.schema.paths).forEach(key => {
projection[key] = 1;
});
projection.subfield = { $elemMatch: { _id: subfieldId } };
Model.find({}, projection).then((result) => console.log({ result });

MongoDB indexing multi-input search

I have a search function that looks like this:
The criteria from this search is passed through to MongoDB's find() method as a criteria object, e.g:
{
designer: "Designer1".
store: "Store1",
category: "Category1",
name: "Keyword",
gender: "Mens",
price: {$gte: 50}
}
I'm only just learning about indexes in MongoDB so please bear with me. I know I can create an index on each individual field, and I can also create a multi-index on several fields. For instance, for one index I could do:
db.products.ensureIndex({designer: 1, store: 1, category: 1, name: 1, gender: 1, price: 1})
The obvious issue arises if someone searches for, say, a category, but not a designer or store it won't be indexed.
I'm currently looking up these terms using an $and operator, so my question is:
How can I create an index that allows for this type of searching with flexibility? Do I have to create an index for each possible combination of these 6 terms? Or if I use $and in my search will it be enough to index each individual term and I'll get the best performance?
$and won't work as MongoDB can only use one index per query at the moment. So if you create an index on each field that you search on, MongoDB will select the best fitting index for that query pattern. You can try with explain() to see which one is selected.
Creating an index for each possible combination is probably not a good idea, as you'd need 6 * 5 * 4 * 3 * 2 * 1 indexes, which is 720 indexes... and you can only have 63 indexes. You could pick the most likely ones perhaps but that won't help a lot.
One solution could be to store your data differently, like:
{
properties: [
{ key: 'designer', value: "Designer1" },
{ key: 'store', value: "Store1" },
{ key: 'category', value: "Category1" },
{ key: 'name', value: "Keyword" },
{ key: 'gender', value: "Mens" },
{ key: 'price', value: 70 },
]
}
Then you can create one index on:
db.so.ensureIndex( { 'properties.key': 1, 'properties.value': 1 } );
And do searches like:
db.so.find( { $and: [
{ properties: { $elemMatch: { key: 'designer', value: 'Designer1' } } },
{ properties: { $elemMatch: { key: 'price', value: { $gte: 30 } } } }
] } )
db.so.find( { $and: [
{ properties: { $elemMatch: { key: 'price', value: { $gte: 45 } } } }
] } )
In both cases, the index is used, but only for the first part of the $and element right now. So do check which key type has the most values, and order your $and elements accordingly in the query.