I have a document called Mapping, which has an _id and an array of objects called Mappings.
I have another collection called NewMappings. For each _id in NewMappings, I need to search within the array of Mappings (of Mapping collection) and return the _id of Mapping.
I wrote something like this, but it failed to return anything.
var d=db.NewMappings.find();
d.forEach(function(item){
db.matching.find({Mappings: {$elemMatch : {TargetId: item._id}}})
})
however, this query returned values
var d=db.NewMappings.find();
db.matching.find({Mappings: {$elemMatch : {TargetId: d[0]._id}}})
Am I missing something?
Please help me. I am in dark. thanks in advance.
One way, if you just want to see the results is:
var d=db.NewMappings.find();
d.forEach(function(item){
db.matching.find({Mappings: {$elemMatch : {TargetId: item._id}}}).forEach(printjson)
})
You can also use the aggregation framework
Sounds like you're trying to do a Join, which isn't explicitly supported in MongoDB. You'll need to either use a few aggregation functions to flatten the data or a mapreduce.
Here's an example using mapreduce to restructure information:
http://cookbook.mongodb.org/patterns/pivot/
Related
Hi guys I have a problem with a multiple query in the firebase list
This query filters requests by these queries
But it brings me three results and correct there are only two results
this.all_being_executed = this.fdb.list('main_requests', ref => {
let query = ref.orderByChild('requests_receiving_status').equalTo(false);
query = ref.orderByChild('requests_status').equalTo(true);
return query;
}).valueChanges();
return this.all_being_executed;
Is there any help to filter the results correctly
You can use only one orderByChild() clause in your query, so it seems like your second clause is overwritting the first one and you're just getting the ones with request_status===true.
From the documentation:
Queries can only order by one key at a time. Calling orderByChild()
multiple times on the same query throws an error
My suggestion is to either refactor your current data structure to allow for a simpler query, or use a new third field which concatenates both requests_receiveing_status and request_status, them do your query on this concatenated field. Something like this:
{
"requests_receiving_status_and_status": "false_true";
}
and query it like this:
let query = ref.orderByChild('requests_receiving_status_and_status').equalTo('false_true');
Now, I know that this seems hacky and non-performant, but it is actually a valid approach for Firebase.
But, if you prefer a cleaner solution, try to separate your objects on two separated lists, one for requests_receiving_status true and the other for the false ones, them search only on the false ones using query = ref.orderByChild('requests_status').equalTo(true);. It adds redundancy but it is another valid approach on NoSql databases.
Also, check out this video and see if it helps you to tackle this issue.
I'm trying to use the ES6 String template to query my database
My SQL is like :
SELECT * FROM table WHERE id IN ();
The in must contains each items of an array, so, i tried something like this :
return connector.query`SELECT * FROM table WHERE AnalogHistory.TagName IN (${sensor})`.then(result => {return result.recordset});
This doesn't work. But if i try something like this :
return connector.query`SELECT * FROM table WHERE AnalogHistory.TagName IN (${sensor[0]},${sensor[1]}, ${sensor[2]}, ...)`.then(result => {return result.recordset});
Well this time, it work. So do you guys know what differ and on to do it by the first way (the cleaner) ?
We can't be absolutely sure without knowing how query handles the tag parameters, but the difference is that in the first case, you've provided query with the array as a single parameter; and in the second case, you've provided query with a series of individual parameters with commas in-between them.
Apparently it's happy with the second, and not with the first. Whether there's a third option that would be less laborious but also functional depends entirely on how query is designed to be called.
Re your comment saying it's the mssql npm package: I don't see anything in its documentation to suggest that it supports passing arrays into the query tag function as parameters. Which is really unfortunate.
In a Node.js App, i want to fetch some data from database via Mongoose. i do it like this:
Users.find({_id:userID}).exec(function(err, users){
});
because users is an Array like Mongoose object, i cant do this:
users.toJSON();
this works if i use findOne but right now doesn't work. also users.toObject() doesn't work and i get this error:
object has no method 'toObject'
if i use lean() in query like this:
Users.find({_id:userID}).lean().exec(function(err, users){
});
this works but it has other problems. for example, if an array value has no value, instead of showing it like this:
myval:[]
doesn't show that key/value pair at all!!
What i want is that i have to edit that users result and apparently, its Mongoose object and i cant. because of that i have to convert it to a regular JSON but how?
Just use findOne instead of find and you'll be able to edit your instance.
If you absolutely have to use find then know that users parameter is the array of user instances, so just loop through them and edit them one by one.
Users.find({_id:userID}).exec(function(err, users){
users.forEach(function(user){
// edit my user here
});
});
I'm interested in using the visualsearch.js control for my website but, having read through the documentation, I am still unclear regarding how to effectively obtain the output search collection data. Based on the example, the output string is constructed through serialization of the search collection. However, I was wondering if there is a way to access the search collection in a more array-like fashion (so that for/in loops can be used) rather than having to parse a single serialized string. Ultimately, I need to construct SQL queries from the search collection data.
If there is an even more efficient or appropriate way of accessing the search collection data, please let me know!
Thanks!
as far as i know there are 2 ways to fetch data from visual search
it is also directly explained in their documentation in usage #4
like you said, the stringified version of the search.
visualSearch.searchBox.value();
// returns: 'country: "United States" state: "New York" account: 5-samuel title: "Pentagon Papers"'
or the facetted object to loop over
visualSearch.searchQuery.facets();
// returns: [{"country":"United States"},{"state":"New York"},{"account":"5-samuel"},{"title":"Pentagon Papers"}]
as you can see, this option gives you an array, per facet that was filtered on, and for each asset the value that was entered.
mhmmm.. ok, the answer is not so straightforward. I would suggest you to get some practice with backbone structure just making some modification to the todo-list app. It is a great startpoint. So you get familiar with some of the wonderful backbone.js methods for collections
The Basic idea is the following:
With visualsearch you can obtain a list of "facets", that is to say an array of key/values objects.
var myFacets = visualSearch.searchQuery.facets();
//my facets is then something like [{"field1":"value1-a"},{"field2":"value2-c"}]
after this you can use myFacets elements to iterativrely filter you collection with the WONDERFUL filter method hinerithed from _underscore lib.
How to do it? You can use the _.each method in the underscore lib
_.each(myFacets,function(facet){
myCollection=myCollection.filter(function(item){
return item.get(facet.get('category')) == facet.get('value');
});
});
}
Here you use the filter method of backbone.js, which returns only the values are true according to your clause. So, you filter your collection once for each single facet. It is like telling to javascript: "Return me only the elements of the collection which match with this facets (value)", and you do it iteratively for all the different facets you got.
Hope this helps.
Ah.. one last thing, just to mess ideas up :-) :Visualsearch is built on backbone.js, and the searchQuery object is nothing but a backbone Collection, so you can use the methods and the properties of the basic backbone collection. Read this line again if this is not clear, because this can be a key point for future implementations! :-)
I suggest you to have a look at the search_jquery.js file in the lib/js/models folder. It's very interesting...
I tried to find the answer a lot but no way. I have an Ext.data.ArrayStore store and want to get its data as string. I tried store.getRange(), store.getAt() but I couldn't figure out what these functions return. Is there any way to get ArrayStore data as string?
I am newbie to extjs, so if you have any example on this, I'd appreciate.
It really depends what you want to do with the data. For most UI widgets and that sort of thing, you'll want to just use the store directly. If you want to get a piece of data from the store for tweaking manually, that's a whole nother story.
store.getRange() will indeed return all of the records from the store, but they are returned as an Array of Record objects. Records contain an attribute called data which is an object containing any properties you defined in the record's config.
Example:
Ext.each(store.getRange(), function (item, idx, a) {
for (var i in item.data) {
console.log(item.data[i])
}
})
That should show you every item in every Record in store
EDIT: Changed my answer to not be totally wrong.