This question already has answers here:
MongoDB field order and document position change after update
(8 answers)
Closed 5 years ago.
I have this object and query
Object
{
_id: 1,
name: 'John Smith',
items: [{
name: 'item 1',
value: 'one',
_id: 1
},{
name: 'item 2'
value: 'two',
_id: 2,
}]
}
Query
Person.update({'items._id': 2}, {'$set': {
'items.$.surname': 'new surname'
}}, function(err) { ...
I want that the new updated (key / value) to be setted on first position like this
,{
surname: 'new surname',
name: 'item 2'
value: 'two',
_id: 2,
}
not like this
,{
name: 'item 2',
value: 'two',
_id: 2,
surname: 'new surname',
}
So is there a solution,
Thanks in advance
As far as I know if you append a property to an object that wasn't previously there it will always be appended to the object. The only workaround is to set the surname in the object to start with.
Related
I just started learning mongoDB and mongoose here.
Is it possible to have value as key in mongoDB? For example, I'd like to have a structure like this:
Person collection:
USA: {
'John Doe': { phone: '...', somethingElse: '...' },
'Jane Doe': { phone: '...', somethingElse: '...' },
},
Australia: {
'John Doe': { phone: '...', somethingElse: '...' },
'Jane Doe': { phone: '...', somethingElse: '...' },
},
England: {
'John Doe': { phone: '...', somethingElse: '...' },
'Jane Doe': { phone: '...', somethingElse: '...' },
}
I know it's a terrible example, and I understand alternatively we can store the documents like:
{_id: 1, name: 'John Doe', address: 'USA', phone: '...', ...},
{_id: 2, name: 'John Doe', address: 'Australia', phone: '...', ...},
{_id: 3, name: 'John Doe', address: 'England', phone: '...', ...},
I guess I'm just trying to understand if storing value as key is even possible here. And if it is, how do we define the schema with mongoose?
Theoretically you could use a schema like:
const testSchema = new Schema({
countries: {
type: Map,
of: {
type: Map,
of: Object,
},
},
});
taking advantage of Map type in mongoose, then you can assign your dynamic object to contries property.
Personally I believe that second approach you mentioned is a way better idea (unless you really have a good reason for using the first one).
First reason is that having dynamic key names makes querying difficult. Instead of simple .find({name: 'John Doe'}) you need to run complicated aggregation queries like here. Basically any traversing like counting people, filtering by phone etc will be painful with dynamic keys.
Second reason is that MongoDB document has 16MB limitation so gathering too many people means you can approach that limit.
This question already has answers here:
Sort a list of objects in Flutter (Dart) by property value
(12 answers)
Closed 3 years ago.
I need to sort a list in Flutter, that's an object List, I have a model and that model has a property named as isFeatured, I need that all the elements with isFeatured as true be in the first positions of the List.
I mean I could have something like:
[
{
id: 1,
name: 'Test',
isFeatured: false,
},
{
id: 2,
name: 'Test 3',
isFeatured: true,
},
{
id: 3,
name: 'Test 5',
isFeatured: false,
},
{
id: 4,
name: 'Test 34',
isFeatured: true,
}
]
And the elements with isFeatured as true should be in the first position.
you can use sort method with custom comparator for bool
myList.sort((a, b) => (a.isFeature ? 1 : 0) - (b.isFeature ? 1 : 0));
Use List.sort
var myList = [
{
'id': 1,
'name': 'Test',
'isFeatured': false,
},
{
'id': 2,
'name': 'Test 3',
'isFeatured': true,
},
{
'id': 3,
'name': 'Test 5',
'isFeatured': false,
},
{
'id': 4,
'name': 'Test 34',
'isFeatured': true,
}
];
myList.sort((a, b) => (b['isFeatured']?1:0).compareTo(a['isFeatured']?1:0));
Let suppose this is my data set
[
{
id: 1,
roomId: 'abc',
type: 'user-message',
message: 'I am a message',
date: DATE_ISO_FORMAT
}, {
id: 2,
roomId: 'abc',
type: 'bot-message',
message: 'I am a bot message',
date: DATE_ISO_FORMAT
}, {
id: 3,
roomId: 'abc',
type: 'user-message',
message: 'I am another message',
date: DATE_ISO_FORMAT
}, {
id: 4,
roomId: 'abc',
type: 'system-message',
message: 'I am a system message',
date: DATE_ISO_FORMAT
}, {
id: 5,
roomId: 'abc',
type: 'bot-message',
message: 'I am another bot message',
date: DATE_ISO_FORMAT
}, {
id: 6,
roomId: 'xyz',
type: 'user-message',
message: 'I am user message from another room',
date: DATE_ISO_FORMAT
}
]
I want to retrieve the latest message of each roomId where the last message is not bot-message, so if the last message was bot-message I would want to get the second latest message where the message might of type user-message or system-message. I know this will be done with some sort of aggregation, any idea or starting point where I can start this from.
Any help is highly appreciated.
Complete Query
db.collection.aggregate([
{$match:{type:{$ne:'bot-message'}}},
{$sort:{date:-1}},
{
$group:
{_id: "$roomId","date": { $first: "$date" },
"type":{$first:"$type"},"message":{$first:"$message"}}
}
])
I'm trying to make something similar to Trello.com. There will be many boards, and each board can have many lists, and each lists can have many items. The order of the lists and items can change often by the user, so I came up with something like this that would minimize queries. I just read some of the mongo documentation last night so I would appreciate any feedbacks and suggestions
Here is a list of boards:
{
boards: [
{
_id: 901292190
name: 'board1'
}
]
}
Here is when you view a single board:
{
_id: 901292190
name: 'board 1',
lists: [
{
_id: 932092,
name: 'todo',
items: [
{
_id: 9320903,
name: 'go shopping',
priority: 1,
createdby: 'user1',
assignedto: 'user2'
},
{
_id: 3902901,
name: 'go to school',
priority: 2,
createdby: 'user1',
assignedto: 'user2'
}
]
},
{
name: 'finished',
items: [
{
_id: 91209,
name: 'programming',
priority: 1,
createdby: 'user1',
assignedto: 'user2'
}
]
}
]
}
I have a Users Collection and a Rooms Collection, the rooms collection stores references to the user id of the users in the room.
users = [
{_id:1, name: 'name 1', type: 1},
{_id:2, name: 'name 2', type: 1},
{_id:3, name: 'name 3', type: 1},
{_id:4, name: 'name 4', type: 1}
];
room = {
name: 'example room',
connected_users: [
{user_id: '2', admin: false},
{user_id: '3', admin: true}
]
};
Is there a way to search in the Users collection for the details of the connected users like this or similar?
db.users.find({_id: [2,3]});
That returns
[{_id:2, name: 'name 2', type: 1}, {_id:3, name: 'name 3', type: 1}]
That's what the $in operator is for:
db.users.find({_id: {$in: [2,3]}});
db.Users.find({_id: {$in: [2,3]}}).toArray();