I want to assign fields with mongoengine? - mongodb

I have the following query:
Family.objects('name': 'bob')
which returns:
{'_id': 22, 'name': 'bob', 'age': 12, 'height': 176}
But I just need age
I can do it with mongodb:
db.family.find({'name': 'bob'}, {'age': 1})
But, how can I do it with mongoengine?

What you want is the only() method.
Family.objects(name='bob').only('age')
If you use get then use . to access fields' values
Family.objects.get(name='bob').age

Related

Can multiple documents be updated by multiple documents in mongo or mongoose?

I have a variable which is an array of documents. I want to update these documents in mongo. I'm using mongoose for ORM.
For example I have these objects in mongo:
[{name: 'John', age: 10}, {name: 'Mary', age: 12}]
and I want to update them with:
[{name: 'John', age: 11}, {name: 'Mary', age: 13}]
where name is the key, so it's a many-to-many function.
Can this be done in single query?

How to fetch only few fields from all the MongoDB records [duplicate]

This question already has answers here:
How to select a single field for all documents in a MongoDB collection?
(24 answers)
Closed 3 years ago.
I'm new to MongoDB work and I need help in the following.
I have a collection named student in MongoDB. And I have the following data in it.
{'_id': 1, 'name': 'A', 'dt_joined': '2010'}
{'_id': 2, 'name': 'B', 'dt_joined': '2011'}
{'_id': 3, 'name': 'C', 'dt_joined': '2009'}
{'_id': 4, 'name': 'D', 'dt_joined': '2010'}
{'_id': 5, 'name': 'E', 'dt_joined': '2008'}
From the above collection, I want to retrieve (_id, dt_joined) from all the records. That means, I'm expecting the following result.
{'_id': 1, 'dt_joined': '2010'}
{'_id': 2, 'dt_joined': '2011'}
{'_id': 3, 'dt_joined': '2009'}
{'_id': 4, 'dt_joined': '2010'}
{'_id': 5, 'dt_joined': '2008'}
Is it possible with the MongoDB find command?
Thanks in advance!!
try this
db.student.find({}, {_id:1, dt_joined: 1 })
for more details check official site - https://docs.mongodb.com/manual/reference/method/db.collection.find/
You can use one of the following.
db.student.find({}, {_id:1, dt_joined: 1 })
db.student.find({}, {name: 0 })

Can I sort the values in a field in mongodb?

If I have a document like this
{'id': 123, 'keywords': {'keyword1': 3, 'keyword2': 8, 'keyword3': 2}}
Can I get a query result with keywords sorted by values, like
{'id': 123, 'keywords': {'keyword3': 2, 'keyword1': 3, 'keyword2': 8}}
Is there a built-in mongodb feature for doing this? Or I have to implement this in application level?

Mongo DB Grouping Queries

I want to be able to retrieve a list of documents matching an id and trace the results back to a specific key.
Example:
// I have these objects:
[{_id: 1, group: 'a'}, {_id: 2, group: 'b'}, {_id: 1, group: 'c'}]
// and these database documents:
{_id: 1}, {_id:2}
I want to create a query that would return essentially {_id: 1}, {_id: 2}, {_id: 1}, or any other result where I can map back the find result to the 'group' property of the original objects.
The problem I run into is that if the list of id contains duplicates or keys not found in the target collection, I lose ordering and can't map the query result back to my objects.
I thought $group could achieve this, but I haven't been able to achieve this so far.
To get a list of all documents matching an id, wouldn't a simple find work?
db.collection.find({"_id":1})

Mongo shell, how to find/aggregate embedded documents with lists

I'm trying to aggregate/find data on subdocument that have lists of documents....
I have a mongo document that looks similar to this,
{'town-name': 'anyplace ville',
'locations': {
'sports': [
{'name': 'football world', 'audience': 10},
{'name': 'abc', 'audience': 8},..
],
'food': [
{'name': 'pizza world', 'audience': 25},
{'name': 'm&ms', 'audience': 63},..
],
}
}
How can I find/aggregate the sub document of 'sports' or 'food' whose items are lists of documents?
For example I'm trying to find the town-name's of places where 'sports' audiences are greater than 10 or 'food' names are equal to 'm&ms'?
The following query lists the town-name's where sports audiences are greater than 10.
db.test.find({"locations.sports.audience" : {$gt:10}},{"town-name":1})
This one will list the town-name's where food names are equal to m&ms
db.test.find({"locations.food.name" : "m&ms"},{"town-name":1})