MongoDB query

1

I am trying to make a query in MongoDB , and it is not clear to me how to do it.

Through the following line, I get what is shown:

I would like to know if there is any way to obtain only the field in question, ie "qfmSLTmr3tyZwykaA" and not everything that precedes it.

Or treat it in some way through javascript

    
asked by Mario López Batres 31.01.2017 в 18:52
source

2 answers

1

When you perform a search in a collection and wish to exclude one or several fields you can use 0 to indicate not to show the field:

db.colleccion.find({}, {"nombre campo no requerido": 0 })

to include it we use 1

db.colleccion.find({}, {"nombre campo no requerido": 1 })

In the case of what you do, you get all the elements of the collection but only show the "building" field, while the "_id" field excludes it:

db.terminales.find({}, {"edificio":1, "_id":0});

with this result:

"edificio": "qfmSLTmr3tyZwykaA"

If you want to obtain only the value (or the values of a certain field) you get it this way:

db.terminales.find().forEach(function(row) {print(row.edificio);})

with this you will get only the value of the field edificio

qfmSLTmr3tyZwykaA
    
answered by 31.01.2017 / 18:58
source
0

Yes you can. This query method is called Projection . The method find accepts two objects as a parameter:

  • Evaluation fields
  • Projection
  • Your query will look like this when using Projection :

    db.terminales.find({}, { eficio: 1 } );
    

    The integer is binary, that is, accepts only 0 and 1 . By default, the _id will also be selected, in case you do not want this behavior you can skip it by setting 0 to _id :

    db.terminales.find({}, { eficio: 1, _id: 0 } );
    
        
    answered by 31.01.2017 в 19:48