I have trouble to slice an array which is nested inside an object. I'm new to mongo and not sure if the model I choose for the document is most elegant one for my case.
Let's say I have:
_id: 1
state: {
state: AZ
cities: ["a", "b", "c", "d"]
}
I tried:
1) db.collection('states').aggregate([
{ $match: { state: 'AZ' } },
{ $project: { membersArr: { $slice: ['$cities', 0, 1] } } },
]);
2) db
.collection('states')
.findOne({ 'state.state': 'AZ' }, { $slice: ['$cities', 0,1]})
Expected result: array of cities will be sliced!
Please help. Thank you!
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…