Accessing attributes added via $meta in a MongoDB query -
i'm having difficulty accessing attributes added when performing weighted search:
post.collection .find( { $text: { $search: 'whatever }}, { score: { $meta: 'textscore' } } ) .sort( { score: { $meta: 'textscore' } } ) .exec(function(error, results) { console.log(results[0].score) // returns undefined })
if console.log individual result, can see score
attribute:
{ title: 'test post 6', content: 'keyword keyword keyword keyword keyword keyword', score: 3.9375, __v: 0, slug: 'test-post-6', _id: 569a4f33fad6dfc724e02508 }
but accessing value via .score
or key ['score']
returns undefined
.
how can access score
meta attribute?
i using underscore in code, leveraged _.getkeys()
view of keys on result. there's 2 keys, get()
, getvalue()
available can used return score
:
post.collection .find( { $text: { $search: 'whatever }}, { score: { $meta: 'textscore' } } ) .sort( { score: { $meta: 'textscore' } } ) .exec(function(error, results) { console.log(results[0].getvalue('score')) })
just reference, here available keys on mongodb 3.0.8 result object:
[ '$__', 'isnew', 'errors', '_doc', '$__original_save', 'save', '_pres', '_posts', 'db', 'discriminators', '__v', '_', 'list', 'content', 'title', 'slug', 'id', '_id', 'schema', 'collection', 'getupdatehandler', '$__handlesave', '$__save', '$__delta', '$__version', 'increment', '$__where', 'remove', 'model', 'on', 'once', 'emit', 'listeners', 'removelistener', 'setmaxlisteners', 'removealllisteners', 'addlistener', '$__builddoc', 'init', '$__storeshard', 'hook', 'pre', 'post', 'removepre', 'removepost', '_lazysetuphooks', 'update', 'set', '$__shouldmodify', '$__set', 'getvalue', 'setvalue', 'get', '$__path', 'markmodified', 'modifiedpaths', 'ismodified', '$isdefault', 'isdirectmodified', 'isinit', 'isselected', 'validate', '$__validate', 'validatesync', 'invalidate', '$markvalid', '$isvalid', '$__reset', '$__dirty', '$__setschema', '$__getarraypathstovalidate', '$__getallsubdocs', '$__registerhooksfromschema', '$__handlereject', '$toobject', 'toobject', 'tojson', 'inspect', 'tostring', 'equals', 'populate', 'execpopulate', 'populated', 'depopulate', '$__fullpath' ]
Comments
Post a Comment