Find a document in a nested query mongodb - node.js

I have two collection A and B where in B there is a field that has the id of A as shown in the below. The code below returns nothing.
So I am trying to figure out if I want to use mongodb shell, how can I use the return cursor of A in order to point to ID values and send it to the value of AID key.
db.B.find({
'AID': db.A.find({'item': 'x'},
{'id': 1})
});

Related

Get multiple documents from collection using nodejs and mongodb

Hi I have two mongodb collections. The first one returns json data (array) and with the output of this, I want to return documents that match.
When I run Console.log (req.bidder.myBids) I get the following output:
[{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3010},{"productId":"3798b537-9c7b-4395-9e41-fd0ba39aa984","price":3020},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1040},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1050},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1060},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1070},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1090},{"productId":"4c4bd71c-6664-4d56-b5d3-6428fe1bed19","price":1100}]
The productId has duplicates, I want to remove duplicates and then call a routine that finds all the products that match and output as json.
So far I have this code that only outputs one document, but cant figure out how to add the array of productId's and then fetch all corresponding products.
var agencyId = req.body.agencyId;
var productId = req.body.productId;
if (!validate.STRING(agencyId)) {
res.apiError(messages.server.invalid_request);
} else {
dbProduct.find({productId:{$in:['3798b537-9c7b-4395-9e41-fd0ba39aa984','4c4bd71c-6664-4d56-b5d3-6428fe1bed19']}
}).then(dbRes => {
console.log(dbRes);
Updated code and works with hard-wired productId and updated above code. Looking at how to get the array data and transpose replacing the hard-wired productId's
The $in operator is what you want. See the docs here: https://docs.mongodb.com/manual/reference/operator/query/in/

Getting index of the resultset

Is there a way to get the index of the results within an aql query?
Something like
FOR user IN Users sort user.age DESC RETURN {id:user._id, order:{index?}}
If you want to enumerate the result set and store these numbers in an attribute order, then this is possible with the following AQL query:
LET sorted_ids = (
FOR user IN Users
SORT user.age DESC
RETURN user._key
)
FOR i IN 0..LENGTH(sorted_ids)-1
UPDATE sorted_ids[i] WITH { order: i+1 } IN Users
RETURN NEW
A subquery is used to sort users by age and return an array of document keys. Then a loop over a numeric range from the first to the last index of the that array is used to iterate over its elements, which gives you the desired order value (minus 1) as variable i. The current array element is a document key, which is used to update the user document with an order attribute.
Above query can be useful for a one-off computation of an order attribute. If your data changes a lot, then it will quickly become stale however, and you may want to move this to the client-side.
For a related discussion see AQL: Counter / enumerator
If I understand your question correctly - and feel free to correct me, this is what you're looking for:
FOR user IN Users
SORT user.age DESC
RETURN {
id: user._id,
order: user._key
}
The _key is the primary key in ArangoDB.
If however, you're looking for example data entered (in chronological order) then you will have to have to set the key on your inserts and/or create a date / time object and filter using that.
Edit:
Upon doing some research, I believe this link might be of use to you for AI the keys: https://www.arangodb.com/2013/03/auto-increment-values-in-arangodb/

Node mongo bulk update, get the updated columns name

i have a bulk updation on mongodb in nodejs
i am facing the following scenario
user can update 20-30 fields at once, possibly he dont update all, he may change 1 or 2 or 5 or maybe all, i want to know exactly which are fields ie, whose value have be over-written with new values.
i need to know this because every field carry a weightage and if they update a particular field then i have to re-calculate the weightage of all the columns and assign it to the user
for example if
User.update({_id: user_id},{ $set : {key1:value,key2:value,key3:value... }})
now suppose key3 is over-written but key1 & key2 remains as it is,
so i want to know that key3 has been over-written.
how can i do that ?
if you use findOneAndUpdate function for updating, it's possible to retrieve document before updating, with that retrieved value you then can compare it with given document to find out witch fields has been updated.
The update function will be like this:
db.collection("Collection Name").findOneAndUpdate({"Find Query"},
{$set: {"Update Query"}},
{returnOriginal: true},
(err, res) => {
//... Some codes for comparing docs
});
The "returnOriginal" option makes the query to return updated document before update operation.

mongodb querying logic mongoose and nodejs

I apologize beforehand if this is a repeat question but i could not find any and i am a beginner.
I am making a filter for documents of db.
lets say I want to query 8 fields of documents written in a object
filter={key1:{lowerlimit: 'value', upperlimit:'value'},
key2:{lowerlimit: 'value', upperlimit:'value'},
key3:{lowerlimit: '', upperlimit:'value'},
key4:{lowerlimit: 'value', upperlimit:''},
key5:[],
key6:['value','value'],
key7:['value','value','value'],
key8:['value']}
now how do i write the logic of query so that the fields whose value is not specified will not get query. if i query all the parameters as they are. no documents will be displayed because 'key5' is empty array and using $in on it will result in no documents.
You'd want to remove the key5 field from your query object when it's empty.
if (!filter.key5.length) {
delete filter.key5;
}

How to filter by document attributes in couchdb?

Could someone explain to me how I can filter documents with multiple attributes by using arrays and keys?
For example I have a document with the attribute a, b, c and d. I would like to filter by an user selected value from attribute "a". Later I would like to narrow the results with a value from the attribute "c" or maybe a value from the attribute from "d".
Does anyone have suggestions how to accomplish this task elegant?
Assuming your doc looks like:
{ 'a': 123, 'b': 456, 'c': 789, ... }
You can create a view like this:
function(doc){
emit([doc.a, doc.b, doc.c], doc)
}
You can then use the startkey and endkey parameters to access the views, whilst restricting results to a specific subset:
...&startkey=[123,]&endkey=[123,{}] // Shows all results with doc.a=123
...&startkey=[123,]&endkey=[123,456] // Shows all results with doc.a=123 and doc.b<=456
However all elements will be sorted in a single list and all you can ever access is a subsection of this list. So if you want to access documents where 123 <= doc.a <= 456 and doc.b between 123 and 456, you'll have to create two separate views, one for doc.a and one for doc.b and then have your client app identify the documents returned by both views.

Resources