Using Where With SubSonic Collection - subsonic

I am having problem with collection object. Here is the code
CarCollection obj=new CarCollection();
obj.Where("Id","10");
obj.Load();
The problem is the result of the records doubles i.e actually there is only 1 record with the id 10 but it returns 2 same records. Please Help me as i am a newbie.
Thanks

You should be able to do this with the following query:
CardCollection cards = new Select()
.From(Card.Schema)
.Where(Card.IdColumn).IsEqualTo(10)
.ExecuteAsCollection<CardCollection>();
I'd suggest you have a look at the query docs to see some examples of SubSonic queries:
http://subsonicproject.com/docs/Simple_Query_Tool

Related

Prob find latest collection

I don't find much information about this problem to solve.
On my mongodb I create a collection every 60 seconds with the name "test "+ date.now(). So far everything works ok. It creates me different collections with the name test XXXXXX1, test XXXXX2 etc.
I have problems with the mongoose.find() method. I can't find my last created collection.
let test = mongoose.model('test' + date.now(), Schema);
test.find({}, function (err, response) {});
How do I find the latest collection in stream? Thank you!
Mongo ,By default , does not support sequence .
for that purpose you're going to have to add specific field for sorting or sort your fields based on your current field properties.
After that you have to use .sort() cursor method :
Collection.find().sort([...]);
Read this article for more info

I can´t find right values querying nested arrays with mongodb, mongoose and nodejs

Dear stackoverflow community,
I usually no make posts here, but in this time i ask myself why not?, after 2 hours without getting a solution.
This one is pretty basic but involves a specific json model, mongoose, mongodb and nodejs.
Now the problem, is that i´m trying to get the data for the user_ppal_id : 1 and the exam_design_id : 1 , the search of the user_ppal_id succeeded, but the exam_design_id that is into a nested array always returns to me the value 1 and 2 (and i´m passing by parameter the exam_design_id 1)...
This is my model, My nodejs rest method and how i´m using mongoose, nodejs and mongodb, My moongose model, A better view of the model from Umongo Ide:
SEE THE IMAGE HERE BECAUSE I AM NEW IN STACKOVERFLOW
All the structure is working, but the data from result is not the one i expected. You can check the response of this rest service by just clicking this url: http://207.244.75.230:8000/dyntestreports/getExamReport?idUserPpal=1&idExamDesign=1
In the response i´m getting the exam_design_id data for values 1 and 2 when i pass just the value 1.
Actually i tried with the following mongo queries and i not succeed:
1) ExamReportByUser.find({'user_ppal_id':userPpalId,'exams_created': {$elemMatch:{'exam_design_id': {$eq:examDesignId}}}}
2)ExamReportByUser.find({'user_ppal_id':userPpalId,'exams_created.0.exam_design_id':examDesignId}
At this moment i don´t know what i´m doing wrong even from a mongo console query i can´t get it works for just exam_design_id = 1 data.
I´m sure it could be a simple error but i´m not seeing it at this moment.
I really hope you can help me guys. Thanks in advance! Have a nice day.
My bad! I just have found the solution... And i post it here for anyone that had the same doubt... The code querying the data should be like this (it was just keys syntax, basic mongo):
ExamReportByUser.find({'user_ppal_id':userPpalId},{'exams_created': {$elemMatch:{'exam_design_id': examDesignId}}},'id user_ppal_id exams_created exam_design_id user_submitted_exam',function(err, result) { if (err) return console.log(err); return res.send(result); }); };
Now i´m getting the data of user 1 and exam_design 1.
I will not select this as an answer, waiting for the specialists ;)

Updating a many to many join table using sequelize for nodejs

I have a Products table and a Categories table. A single Product can have many Categories and a single Category can have many Products, therefore I have a ProductsCategories table to handle the many-to-many join.
In the example below, I'm trying to associate one of my products (that has an ID of 1) with 3 different categories (that have IDs of 1, 2, & 3). I know something is off in my code snippet below because I'm getting an ugly SQL error message indicating that I'm trying to insert an object into the ProductsCategories join table. I have no idea how to fix the snippet below or if I'm even on the right track here. The Sequelize documentation is pretty sparse for this kind of thing.
models.Product.find({ where: {id: 1} }).on('success', function(product) {
models.Category.findAll({where: {id: [1,2,3]}}).on('success', function(category){
product.setCategories([category]);
});
});
I'd really appreciate some help here, thanks. Also, I'm using Postgres, not sure if that matters.
models.Category.findAll returns an array. By doing setCategories([category]); you are wrapping that array in an array. Try changing it to setCategories(category); instead
I think you are close. I had a similar issue with some of my code. Try iterating over your found categories and then add them. I think this might do the trick.
models.Category.findAll({where: {id: [1,2,3]}}).on('success', function(category){
for(var i=0; i<category.length; i++){
product.setCategories([category[i]]);
}
});

How to query models by a property that is an array

I'm trying to do a 'findOne' operation in a model that has an array property and filter the results to only list the item if the string im searching is in that array.
Example:
var AppUser = server.loopback.getModel('AppUser');
AppUser.create({
"name":"juan"
"favoriteLetters":["a","b","c"]
},function(){
AppUser.findOne({where:{favoriteLetters:'a'}},function(error,appUser){
console.log(error,appUser);
});
});
So in this case i want to find a 'appUser' that has a favorite letter 'a'.
Thanks.
As far as I understood, possibility of such kind of a query depends on the underlying datasource and is not supported yet for relational DBs. But should be fine with memory storage or mongodb. More details and syntax for query is here: https://groups.google.com/d/msg/loopbackjs/8c8kw8EMiPU/yev3lsmrTFUJ
For anyone else who lands here, that query in your model is correct (for Mongo anyways).
{where:{favoriteLetters:'a'}
Reference:
Find document with array that contains a specific value

Subsonic BatchQuery.Queue causing 'Can't decide which property to consider the key...' exception

I'm just getting started with Subsonic 3.0 ActiveRecord and am trying to implement a batch query like the one in the SubSonic docs. I'm using a batch so I can query a User and a list of the users Orders in one shot.
When I call the BatchQuery.Queue() method, adding my "select user" query, SubSonic throws the following exception:
System.InvalidOperationException : Can't decide which property to consider the Key - you can create one called 'ID' or mark one with SubSonicPrimaryKey attribute
The code is as follows:
var db = new MyDB();
var userQuery = from u in db.Users //gets user by uid
where u.uid == 1
select u;
var provider = ProviderFactory.GetProvider();
var batch = new BatchQuery(provider);
batch.Queue(userQuery); //exception here
//create and add "select users orders" query here...
First things first - Why this error? My SubSonic Users object knows it's PK. "uid" is the PK in the database and the generated code reflects this. And I thought SubSonicPrimaryKey attribute was for the SimpleRepository? Is this way of batching not for ActiveRecord?
I could ask a number of other questions, but I'll leave it at that. If anyone can help me figure out what is going on and how to issue 2 batched queries I'd be grateful!
Edit - after further investigation
I ran through the source code with the debugger. Adam is correct - the ToSchemaTable() method in Objects.cs is apparently building out my schema and failing to find a PK. At the very end, it tries to find a column property named "ID" and flags this as the PK, otherwise it throws the exception. I added a check for "UID" and this works!
Still... I'm confused. I'm admittedly a bit lost after peeling back layer after layer of the source, but it seems like this portion of code is trying to build up a schema for my table and completely ignoring my generated User class - which quite nicely identifies which column/property is the PK! It doesn't seem quite right that I'd be required to name all keys "ID" w/ ActiveRecord.
I think the answer you're looking for is that this is a really stupid bug on my part. I'm hoping to push another build next week and if you could put this on the issue list I'd really appreciate it. My apologies...
SubSonic expects your primary key to be called Id so it's getting confused. SubSonicPrimaryKey is for simple repository but I assume where that exception is being thrown is shared between the different templates. If you rename your PK to Id or id or ID your query will work.

Resources