Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongoengine, retriving only some of a MapField

For Example.. In Mongodb..

> db.test.findOne({}, {'mapField.FREE':1})
{
    "_id" : ObjectId("4fb7b248c450190a2000006a"),
    "mapField" : {
            "BOXFLUX" : {
                    "a" : "f",
            }
    }
}

The 'mapField' field is made of MapField of Mongoengine. and 'mapField' field has a log of key and data.. but I just retrieved only 'BOXFLUX'..

this query is not working in MongoEngine.... for example..

BoxfluxDocument.objects( ~~ querying ~~ ).only('mapField.BOXFLUX')

AS you can see.. only('mapField.BOXFLUX') or only only('mapField__BOXFLUX') does not work. it retrieves all 'mapField' data, including 'BOXFLUX' one..

How can I retrieve only a field of MapField???

like image 973
Anderson Avatar asked Dec 02 '25 05:12

Anderson


1 Answers

Try this:

query = BlogPost.objects({your: query})
if name:
    query = query.only('author__'+name)
else:
    query = query.only('author')
like image 143
Ross Avatar answered Dec 03 '25 19:12

Ross



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!