difference between count() and python builtin len()

88 views
Skip to first unread message

aliane abdelouahab

unread,
Oct 5, 2012, 5:30:31 PM10/5/12
to mongodb-user
hi
it's a bizarre thing i got:

db.users.find({"adresse.coord":{"$within":{"$center":[[4.365041,
36.743954], 100]}}}).distinct("produit_up")
Out[17]:
[{u'avatar': {u'avctype': u'image/jpeg',
u'orientation': u'portrait',
u'photo': ObjectId('506f2ee93a5f3a0528ab8621')},
u'spec': {u'abus': 0,
u'date': u'2012-10-05',
u'description': u'brrrrrrrrrrr',
u'id': u'tofla134946378579',
u'namep': u'nokia 6230',
u'nombre': 2,
u'prix': 1000,
u'tags': [u'nokia', u'6230', u'photo'],
u'vendu': False}},
{u'avatar': {u'avctype': u'image/jpeg',
u'orientation': u'portrait',
u'photo': ObjectId('506867863a5f3a0ea84dcd6c')},
u'spec': {u'abus': 0,
u'date': u'2012-09-30',
u'description': u"portable tr\xe8s solide, peu servi, avec batterie
d'une autonomie de 3 heures.",
u'id': u'alucaard134901952647',
u'namep': u'nokia 3310',
u'nombre': 1,
u'prix': 1000,
u'tags': [u'portable', u'nokia', u'3310'],
u'vendu': False}},
{u'avatar': {u'avctype': u'image/jpeg',
u'orientation': u'portrait',
u'photo': ObjectId('506f2b3e3a5f3a0b3c4731a9')},
u'spec': {u'abus': 0,
u'date': u'2012-10-05',
u'description': u'bzzzzzzzzzz',
u'id': u'alucaard134946284638',
u'namep': u'nokia 6230',
u'nombre': 1,
u'prix': 2000,
u'tags': [u'nokia', u'nok', u'noki'],
u'vendu': False}}]

db.users.find({"adresse.coord":{"$within":{"$center":[[4.365041,
36.743954], 100]}}}).count()
Out[18]: 2

len(db.users.find({"adresse.coord":{"$within":{"$center":[[4.365041,
36.743954], 100]}}}).distinct("produit_up"))
Out[19]: 3

Sam Millman

unread,
Oct 5, 2012, 5:36:05 PM10/5/12
to mongod...@googlegroups.com
Distinct returns a single bson document, it's not like SQLs distinct, if I remember right the function outputs three fields in that document, one being the results.


--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb

aliane abdelouahab

unread,
Oct 5, 2012, 5:38:27 PM10/5/12
to mongodb-user
ah! so the reliable is count() ?

On 5 oct, 22:36, Sam Millman <sam.mill...@gmail.com> wrote:
> Distinct returns a single bson document, it's not like SQLs distinct, if I
> remember right the function outputs three fields in that document, one
> being the results.
>

aliane abdelouahab

unread,
Oct 5, 2012, 5:43:41 PM10/5/12
to mongodb-user
because there is "really" 3 products and not "2" !!!

On 5 oct, 22:36, Sam Millman <sam.mill...@gmail.com> wrote:
> Distinct returns a single bson document, it's not like SQLs distinct, if I
> remember right the function outputs three fields in that document, one
> being the results.
>

Russell Smith

unread,
Oct 6, 2012, 12:25:21 PM10/6/12
to mongod...@googlegroups.com
Distinct returns unique values for the specified field

You may (if I recall correctly) be getting duplicated entires if you have more than one addresse field per document, as it may match twice.

Russ
Rainforest | +1-650-919-3216 | rainforestqa.com

aliane abdelouahab

unread,
Oct 6, 2012, 5:52:05 PM10/6/12
to mongodb-user
here it's the coordinates, and because there is a duplicated value
that it has been omited to return 2 instead of 3?

On 6 oct, 17:25, Russell Smith <r...@rainforestapp.com> wrote:
> Distinct returns unique values for the specified field
>
> You may (if I recall correctly) be getting duplicated entires if you have more than one addresse field per document, as it may match twice.
>
> Russ
> Rainforest  |  +1-650-919-3216 |  rainforestqa.com
>
> On Oct 5, 2012, at 2:36 PM, Sam Millman <sam.mill...@gmail.com> wrote:
>
>
>
>
>
>
>
> > Distinct returns a single bson document, it's not like SQLs distinct, if I remember right the function outputs three fields in that document, one being the results.
>

Sam Millman

unread,
Oct 7, 2012, 6:49:29 AM10/7/12
to mongod...@googlegroups.com
Unless distinct works differently on the find of geospatial query then no since the distinct would have have only two documents to distinct then.

aliane abdelouahab

unread,
Oct 7, 2012, 8:41:10 AM10/7/12
to mongodb-user
ah! now i get it,
it seems that mongodb becomes interresting more and more ;)
thank you :)

On 7 oct, 11:49, Sam Millman <sam.mill...@gmail.com> wrote:
> Unless distinct works differently on the find of geospatial query then no
> since the distinct would have have only two documents to distinct then.
>
Reply all
Reply to author
Forward
0 new messages