Please Help.
I am trying to create the query in mongodb like (in MySql):
SELECT * FROM user WHERE user_id = '1' AND (type = 'admin' OR type = 'requester') ORDER BY user_id DESC
How do I convert this query into mongodb find({})?
I'd use $in instead of $or since only a single field is involved:
db.user.find({
user_id:"1",
type: {$in:["admin","requester"]}
}).sort({"user_id":-1});
Check this,
db.user.find({user_id:"1",$or:[{"type":"admin"},{"type":"manager"}]})
Related
I have a use case where I need to join two collections in MongoDB but the join must happen only if one of the two conditions are met.
The SQL equivalent query is given below. How can I translate this in MongoDB?
SELECT *
FROM MainTble i
Left JOIN SecondTable p
ON CASE
WHEN i.myPropert="SMS" and i.phoneNumber = p.phoneNumber THEN 1
WHEN i.myProperty="Email" and i.email = p.email THEN 1
ELSE 0
END = 1
You may use $lookup to achieve a similar behaviour. In the sub-pipeline, use $addFields to create a helper field then $match on the helper field to filter the $lookup / join result.
Here is the Mongo Playground for your reference.
I am trying to create a filter on DBref id field (mongodb). The SQL query generated is given below
SELECT
`part_r_f_q_dpa`.`partRFQId` `part_r_f_q_dpa__partrfqid`,
`part_r_f_q_dpa`.`noOfApproval` `part_r_f_q_dpa__noofapproval`,
`part_r_f_q_dpa`.`CurrentApproved` `part_r_f_q_dpa__currentapprove`
FROM
makethepart.`directPartApproval` AS `part_r_f_q_dpa`
LEFT JOIN makethepart.`partRFQ` AS `part_r_f_q` ON `part_r_f_q_dpa`.partRFQId = `part_r_f_q`._id
WHERE
(`part_r_f_q`.`creatorBuyer.$id` = ?)
GROUP BY
1,
2,
3
ORDER BY
1 ASC
LIMIT
10000
I am getting an error "Error: Unknown column 'part_r_f_q.creatorBuyer.$id' in 'where clause'".
The code excerpt under dimensions in the schema is as below
creatorbuyer: {
sql: `${CUBE}.\`creatorBuyer.$id\``,
type: string
can someone please let me know how should we handle the dbrefs ids as shown above
$id in Mongo BI is referenced as _id. You should use _id instead of $id.
I am using MongoDB with hapi.JS. I have a collection which contains few rows in the schema. I want to sort the rows in either asc order or desc order but want to mention it in the URI. for example the URI should look something like this
/api/v1/customers?sort=name&direction=asc&limit=30
How can I sort this collection by asc or desc order and limit can be fixed or flexible as well.
I have defined like this as of now but even if I mention the sort in URI it gives the output only in asc order.
Models.Account.find(criteria,projection,{skip:5,limit:5},function(err,resp){
if(err)
callbackRoute(err);
else
callbackRoute(err,resp);
}).sort({[_id]:"asc"});
db.yourcollection.find(...).sort({ name:1 }).limit(30)
or with dynamic values:
// following is ECMA 6 only
// get params and make sure values are what you expect (check for injection) + direction must be = "asc" || "desc"
db.yourcollection.find(...).sort({ [sort]: direction }).limit(30)
In MongoDB I have some documents such as these :
{type:'a', ts:123, foo:'bar'}
{type:'a', ts:456, foo:'baz'}
{type:'a', ts:789, foo:'whee'}
{type:'b', ts:123, foo:'jingle'}
{type:'b', ts:456, foo:'bells'}
{type:'b', ts:789, foo:'yeehaaw'}
I would like to find the documents which have the max ts value for each type.
Is it possible to do that with a disngle query ?
I've looked at distinct but apparently it only returns the disting 'type' values.
So I guess I would need a combination of $max alongside distinct ...
But I can't figure the proper way.
Thanks
I'm using mongoose and have a schema similar to this:
var schema = new mongoose.Schema({
created: Date,
fieldA: ObjectId,
fieldB: ObjectId,
fieldC: ObjectId,
sortField: Number
});
This is a big collection so I want to make sure the indexes are optimal. I build up a query with something like this:
var query = Schema.find({created: some-date-clause});
if ( some-condition )
query = query.or({fieldA: {$in: listOfSomeFieldAIDs}});
if ( some-other-condition )
query = query.or({fieldB: {$in: listOfSomeFieldBIDs}});
if ( yet-another-condition )
query = query.or({fieldC: {$in: listOfSomeFieldCIDs}});
query = query.sort({sortField:-1});
I want to make sure that whatever the query is, it's fully covered. My instinct is to create an index that spans created, fieldA, fieldB, fieldC and sortField. But should I actually create several indices for the situations where for example only the second condition is true, or the first and the third are true? Should I be approaching this differently?
Each clause of an $or query is considered independently, so it's likely best to create three separate indexes, one per fieldA, fieldB, and fieldC. See docs here.
The sort occurs after the results of the three $or clauses are merged, so adding sortField to these indexes isn't likely to be useful.
But as always, validate any index approach using explain() to make sure your queries are able to use the indexes you've created.