简体   繁体   中英

MeteorJS : Find and display a value from MongoDB

I am trying a select a value from the Mongo DB just like we do in SQL In SQL we query like :

SELECT column-name FROM table-name WHERE column-name = " something "

In the same way I am trying to do the following in Meteor to access the Mongo DB

collection.find({}, {Col-name1: {Col-name2 : 'xyz'}})

here I am trying to pull an integer value of Col-name1 that corresponds to Col-name2 : xyz

This leads to [object Object] that is a string in the html page. This should rather be a integer value. What am I doing wrong?

Thanks!

You want,

collection.find({Col-name2 : 'xyz'}, {Col-name1: 1})

{Col-name2 : 'xyz'} is the criteria to match and {Col-name1: 1} is to return only Col-name1 field in the document and the document by default will also have _id . If you dont want _id , you have to specify it like {Col-name1: 1, _id: 0}

Also mongodb, doesn't return the field value. It always return as document/s. So, you have to access the field yourself in your code. For example, to access Col-name1 from a returned document, you have to use

document['Col-name1'] 

in your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM