Find several items in one search with MongoDB - mongodb

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();

Related

Retrieve nested MongoDB object after querying nested object

Given the following MongoDB collection groups:
[{
_id: 'g1',
name: 'Group 1',
projects: [{
_id: 'p1',
name: 'Project 1',
tasks: [{
_id: 't1',
name: 'Task 1'
}, {
_id: 't2',
name: 'Task 2'
}]
}]
}]
I need to find task with _id = t2 and return the whole task object:
{
_id: 't2',
name: 'Task 2'
}
I'm able to find the group, either using mongodb:
db.groups.find({ 'projects.tasks._id': ObjectId('t2') })
Or with Mongoose:
Groups.findOne({ 'projects.tasks._id': 't2' })
But of course, this returns the entire group document, and not the task.
I've tried:
db.groups.aggregate([
{
$match: { 'projects.tasks._id': ObjectId('t2') }
}, {
$replaceRoot: {
newRoot: '$chapters.sections'
}
}
])
But this throws an error.

How to query to get specific objects from array of objects?

Currently I am learning mongodb. Suppose I have one collection named post in mongodb which data is :
[{
id: 123,
uId: 111,
msg: 'test 1',
attachments: [
{name: 'attach1', url: 'https://example.com', isDeleted: false},
{name: 'attach2', url: 'https://example.com', isDeleted: true}
]
},
{
id: 456,
uId: 222,
msg: 'test 2',
attachments: [
{name: 'attach1', url: 'https://example.com', isDeleted: true}
]
},
{
id: 789,
uId: 333,
msg: 'test 3',
attachments: [
{name: 'attach1', url: 'https://example.com', isDeleted: false}
]
}]
Now i want the result of all post where attachments.isDeleted is false which look like :
[{
id: 123,
uId: 111,
msg: 'test 1',
attachments: [
{name: 'attach1', url: 'https://example.com', isDeleted: false}
]
},
{
id: 456,
uId: 222,
msg: 'test 2',
attachments: []
},
{
id: 789,
uId: 333,
msg: 'test 3',
attachments: [
{name: 'attach1', url: 'https://example.com', isDeleted: false}
]
}]
I tried this db.post.find({attachments: {$elemMatch: {isDeleted: false}}}) but it is not working. I have taken help from [https://stackoverflow.com/questions/62953855/how-get-query-for-array-of-objects-in-mongodb]
I think this is what you are looking for. It uses the Mongodb aggregation framework. You can take a look the documentation to see details. In brief, the $project stage allow us select fields to show or to hide, and it admits calculated fields. We calculated a new attachments field using $filter stage with a the given condition (isDeleted equals to false).
db.collection.aggregate([
{
"$project": {
_id: 1,
uId: 1,
msg: 1,
attachments: {
"$filter": {
"input": "$attachments",
"as": "attachment",
"cond": {
"$eq": [
"$$attachment.isDeleted",
false
]
}
}
}
}
}
])
You can try it in: https://mongoplayground.net/p/YDvpkbZIZ2H
Note that I changed id by _id, but it was only for style purpose.
Hope I helped.

How to set updated object in first position - Mongo DB? [duplicate]

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.

Mongodb schema for trello

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'
}
]
}
]
}

Updating embedded document in MongoDB

My documents are structured as follows:
{_id: 1, country: 'USA', names: [{language: 'en', name: 'New York', state:'new'}, {language: 'es', name: 'Nueva York', state:'translated'}]}
{_id: 2, country: 'France', names: [{language: 'en', name: 'Paris', state:'new'}, {language: 'it', name: 'Parigi', state:'translated'}]}
...
I want to update the state of an item for a specific language, and if the language does not exist, to add the corresponding embedded document. For example, I would want to update item 1 to set state='new' for language='es' because that language exists:
{_id: 1, country: 'USA', names: [{language: 'en', name: 'New York', state:'new'}, {language: 'es', name: 'Nueva York', state:'translated'}]}
And I would want to add an embedded document to item 2 with state='new' and language='fr' because it doesn't exist:
{_id: 2, country: 'France', names: [{language: 'en', name: 'Paris', state:'new'}, {language: 'it', name: 'Parigi', state:'translated'}, {language: 'fr', name: 'Paris', state:'new'}]}
How can I do that?
Thanks.
You need to use The $ positional operator. See http://www.mongodb.org/display/DOCS/Updating#Updating-The%24positionaloperator for more.
In your case it will be
db.cities.update({'_id':1,'names.language':'es'},{$set:{'names.$.state':'new'}});