I have this mongoose query and I want to add the condition estadoPedido:"Agendado" to it? How can I do it?
Request.countDocuments(
{ estadoPedido: "Pendente", paciente: req.body.paciente }
If I understand you question right, you need to search for documents which have
- estadoPedido = 'Pendente' or 'Agendado'
- and paciente = req.body.paciente
you can use $in operator, something like this
Request.countDocuments({
estadoPedido: { $in: ['Pendente', 'Agendado'] },
paciente: req.body.paciente
})
hope it helps
you can also add multiple where clauses
Request.countDocuments()
.where("estadoPedido")
.in(['Pendente', 'Agendado'])
.where("prop2")
.in(["val1","val2"])
Related
Loopback 4 allows to use an automatic/default filter that is really helpfull. My problem is that I want to use it in a customize way an I am not able.
Usual:
return this.customerRepository.findById(idCustomer, filter);
My Case:
I donĀ“t want to attack the "id" of the model, I want to attack another field. I have tried serveral things, but as a resume an example:
return this.customerRepository.findOne({where: { idUser: currentUserProfile.id }}, filter));
If I do that, the filter stop working. Any idea of how to mix a field in the model different than the id and the filter of loopback 4?
Thanks in advance
Best regards
#Jota what do you mean the filter stopped working? You have the correct idea about the approach. to search by a specific field just put it in the where clause as such:
this.customerRepository.findOne({ where: { <field_name>: <value_to_search> } })
e.g.
const filter = {
where: {
email: 'hello#world.com'
}
};
const result = await this.customerRepository.findOne(filter);
I'm trying to query a database with Sequelize to get items that were created between a certain date range. I used the $between operator but I don't seem to be getting anything.
{ where: {"createdAt":{"$between":["2018-03-31T21:00:00.000Z","2018-05-30T05:23:59.007Z"]}} }
Can anyone help with how I can achieve this?
I also had the same problem, I was getting empty array in response. The problem got fixed using :
const Op = Sequelize.Op;
and then using [Op.between] as shown below:
where: {
createdAt: {
[Op.between]: ["2018-07-08T14:06:48.000Z", "2019-10-08T22:33:54.000Z"]
}
}
Hope it helps :)
$between syntax seems to be right. There are no issues with the way you used. I tried to replicate with the following query
model.findAll({
where: {
created_at: {
"$between": ["2018-03-31T21:00:00.000Z","2018-05-30T05:23:59.007Z"]
}
}
})
The only change is, I use created_at instead of createdAt. Make sure that your column name is right. If it is not, it should have thrown SequelizeDatabaseError. Look for it.
If everything else is right, then you might not be having data in that date range :)
I'm transitioning to ArangoDB for its power as a graph database and am struggling with the simple stuff.
If I have a document...
{ _id:'1234', tags:['apple','orange'] }
how can I update it to push or pull a value from the array? I would expect the syntax to be something like this, but didn't find anything in the docs...
collection.update({ _id:'1234' },{ tags:pull('mango') })
collection.update({ _id:'1234' },{ tags:push('avocado') })
Thank you!
You achieve this with AQL. For example
FOR c in collection UPDATE c WITH { tags : PUSH(c.tags, 'avocado')} IN collection
https://docs.arangodb.com/3.3/AQL/Operations/Update.html
I have a question after performing a find() in mongoosejs.
Is there a better way of just getting the _doc-object without looping through the whole queryresult?
I'm searching for something like model.getDoc() but I can't find it.
Thanks
Ok, I found a solution by myself. Maybe it helps anyone:
var result = model.map(function(obj){
return obj._doc;
});
To get only specific fields of documents use string with field names (separated by space) as a second parametr, if you want ommit specific fields use "-" before field name
Model.find({}, '_doc', function(){...});
But this return documents like this:
{
_id: <object id>,
_doc: <some data>
}
If you want ommit _id field:
Model.find({}, '_doc -_id', function(... {}))
Or alternative syntax:
Model.find({}).select('_doc -_id').exec()
ASchema={item:[BSchema]};
ASchema.findOne({item._id=xx})
It gets a array of BSchema, document.item is a array. how to get only one item which _id is xx?
You want the positional $ operator using query projection to just return your matched array element. For Mongoose you can do this:
ASchema.findOne({"item._id": itemId},"item.$",function(err,doc) {
console.log( doc );
});
Or paired in an object:
ASchema.findOne({"item._id": itemId},{ "item.$": 1 },function(err,doc) {
console.log( doc );
});
Mongoose supports the shorthand syntax with options like "-fieldname" for field removal which is the same as { "fieldname": 0 }. But you cannot mix inclusion and exclusion with the exception of the root _id field.
Therefore you must specify all of the fields you want to appear when using projection.
See also .select() in the mongoose documentation.
I think your syntax for the query is wrong. Try:
ASchema.findOne({'item._id': xx})
This link is helpful for more examples: http://mongoosejs.com/docs/queries.html