Breeze EntityQuery results do not honor projection with select - javascript

When executing a Breeze entityQuery using projection with a .select, the result set still returns every property instead of only those selected, even though the httpresponse only contains the properties in the select.
query = breeze.EntityQuery
.from('MyClass')
.select('name,code,id')
.skip(0)
.take(20);
return manager.executeQuery(query)
.then(function (resp) {
return resp.results;
})
.catch(function (error) {
return error;
});
the httpResponse contains instances with only name, code, and id.....However, the results contains every property for the entity with only those values populated as well as the entityAspect. I was under the impression that the a projection query would contain POJO objects rather than full Breeze entities:
http://www.breezejs.com/documentation/knockout-circular-references
"Construct a query using a select clause that names just the property values you'll display in your grid (plus the entity key so you can get the full entity later when you need to).
A projection query returns JavaScript objects with raw property values."
The issue in the above link is the reason that I am attempting this. I need to bind to a kendo grid (though using AngularJS bindings rather than Knockout).
Thanks!!

Related

How can I use mongoose's "includes"

I want to check my database which contains "likes" field as array type, so I come across this in mongoose documentation but I don't actually know how to use it :
https://mongoosejs.com/docs/api/array.html#mongoosearray_MongooseArray-includes
Here is what I just tried
Products.find({_id:uniqueId}, {$includes:{likes:req.user._id}}, function(err, result){......})
To your image:
It is complaining because you used Products.find. As you are passing in two objects, it expects the second object to be a projection. Projection selects fields from the returned documents to only get only the fields that you need.
Try to get the product with the uniqueId (which I hope is the productId...).
Product.findById(productId, function (err, result) { // callback })
Inside the callback you can check if your userId is included in the likes array.

How can I use variables in an update operation in mongodb that involves an array or another type of embedded or nested document?

The document in the target collection ("myCollection") has a field called "japanese2". This is an array (or an object) that contains an object that contains a property called "japanese2a". The value of this property is initially set to 0 (although it may change later). I want to change this value to 100 with a script in node.js (I am using the Express framework). The database is Mongodb, the free cloud version called Atlas.
If I do this without using variables, it works well:
db
.collection("myCollection")
.updateOne({username:username}, {"$set":{"japanese2.0.japanese2a":100}});
However, if I try this using variables for both the field name, "japanese2", and the name of the element/property in the array/object, "japanese2a", it fails. I have tried the below and other variations but I couldn't find a solution. I have researched stackoverflow for an answer but couldn't find a solution.
There is only one element/property in the array/object to start with.
var field = req.body.fieldName; //want to set this for the field name="japanese2"
var task = req.body.question; //want to set this for the name of the element="japanese2a"
var myField = [task];
field = myField;
var fieldPos = field[0];
.
.
.
db
.collection("myCollection")
.updateOne({username:username}, {"$set":{[fieldPos]:100}});
The above creates a new field called "japanese2a":100" but it does not appear in the array under the field called "japanese2", which is what I want.
I don't get any error messages when I do the above in the console (probably mostly due to the fact that I haven't put in error statements/functions to catch errors), but the output is not correct.
Another way of updating that I found from here:
https://www.codementor.io/#prasadsaya/working-with-arrays-in-mongodb-16s303gkd3
that involves using something like this:
db.posts.updateOne(
{ _id : ObjectId("5ec55af811ac5e2e2aafb2b9"), "comments.user": "Database Rebel" },
{ $set: { "comments.$.text": NEW_CONTENT } }
)
doesn't work for me, as I don't know if the initial value of the element in the array will always be a zero or some other constant. And there is only one element in the array initially. I can only use the username for the match part in the updating. I can't make an expression that is a perfect match for some element in the array.
The update solution from here: MongoDB update data in nested field
db.users.update ({_id: '123'}, { '$set': {"friends.0.emails.0.email" : '2222'} });
works, and that is what I used successfully to update in the first updating attempt above, but I don't know how to incorporate variables into the updating operation, specifically a variable for the field name ("japanese2") that holds the array or the object, and the name of the first and only element/property in the array/object ("japanese2a").
EDITED: I asked for a solution for an "array" originally, but either a field that acts an array (that holds elements that act as objects) or an object (that holds other objects as properties) works in my case, so I edited the question body and title. Also, the accepted solution works with the field as an entity that holds an array, or as an entity that contains an object inside it.
In either case, if there is already a field with the appropriate name, an object is created (if it didn't already exist) as an object of that object called "field" or the array called "field", and the object's property is set as according to the variables in the script.
If the field doesn't exist, it's created (as an object), and this object contains another object that contains the property ("task" as the name and "100" as the value for the name-value pair). So the newly created "field" object contains an object as its property. The property of this object is a name-value pair of "japanese2a" and "100".
If the script is run again, with a different "task" name (eg. "japanese2b"), another property is created, with the name of "japanese2b" and the value of "100". It is created within that same object that is "inside" the "field" object, so the object field.0 (the object within the "field" object) ends up looking like this: {japanese2a: 100, japanese2b: 100}. And the object called "field" looks like this: {{japanese2a: 100, japanese2b: 100}}.
I think something like
var field = req.body.fieldName // japanese2
var task = req.body.question; // japanese2a
var updateObj = { $set : {} };
updateObj.$set[field + '.0.' + task] = 100
db
.collection("myCollection")
.updateOne({username:username}, updateObj);
Might work

How do I access neo4j result properties?

I have a neo4j query in JavaScript that return two sets of nodes:
session
.run ("MATCH (user:Dealer)-[:SUPPLY]->(v) RETURN user,v")
.then (function(result)
{
if ( !result.records[0])
---no records
else {
email=result.records[0].properties.email; //this doesn't work
}
I want to access the properties from both user and v in a nested for loop so I need direct access to each property field. The developers manual tries to explain it but not very well.
The Result returns you a collection of records.
A Record can be compared to a row in the neo4j browser and has a simple get method for accessing record values, in your use case, for having the user :
user = result.records[0].get('user');
When you expect a node or a relationship to be returned, they contain a properties object which represent key value pairs of properties.
A node has also a labels property representing the labels of a node
email = user.properties['email'];
labels = user.labels; // returns the node labels

How to query documents in docpad based on arrays of objects

I have DocPad documents that look like this:
---
categories: [{slug: ""}, {slug: ""}, ...]
---
Document content.
How can I query all documents which have a predefined slug value in the meta.category array?
There's a few ways we can go about this.
Via a Template Helper and Query Engine's setFilter
https://gist.github.com/4556245
The most immediate way is via a getDocumentsWithCategory template helper that utilises Query Engine's setFilter call that allows us to specify a custom function (our filter) that will be executed against each model in the collection and based on it's boolean return value keep the model or remove it from the collection.
The downsides of this solution are:
We have to redefine our category information in each post
We have no immediate method of being able to get information about all the categories available to us
Via Template Helpers and the parseAfter event
https://gist.github.com/4555732
If we wanted to be able to get information on all the categories available to us, but still had the requirement of defining our categories every single time for each post, then we can use the parseAfter event to hook into the meta data for our documents, extract the categories out into a global categories object, and then update our document's categories with id references instead.
Downside is that we still have to have redundant category information.
Via Template Helpers and a global categories listing
https://gist.github.com/4555641
If we wanted to only define our category information once, and then just reference the categories ids within our posts, then this solution is most ideal.
I manage to create a "foo" collection for a given slug value "bar in docpad.coffee:
collections:
foo: (database) ->
database.findAllLive().setFilter("search", (model, value) ->
categories = model.get('categories')
return false unless Array.isArray categories
for category in categories
if (category.slug and category.slug == value)
return true
return false
).setSearchString("bar")
but when I try to create a helper function that returns a collection given the array meta ("categories"), the key ("slug") and value ("bar") of the object :
createCollection: (meta, key, value) ->
result = #getDatabase().createLiveChildCollection()
.setFilter("search2", (model, searchString) ->
objects = model.get(meta)
return false unless Array.isArray objects
for object in objects
if (object[key] and object[key] == searchString)
return true
return false
).setSearchString(value)
i get an empty collection when i try to call it.
I don't have a drop in solution, but have you seen the source code for the blog of docpad's author? There are some examples of querying for the existence of a metadata object attribute. Hope that helps!

Accessing Nested Objects in Json

I have a custom object which contains other items (ie arrays, strings, other types of objects).
I am not sure how to traverse the object to iterate and list all of the object types, keys, and values of the nested items.
Second to this issue I don't know how many levels of nesting there are (as the object is generated dynamically from the back-end and passed to me as one object).
Any ideas (and should I just use javascript/jQuery or both to do this most efficiently)?
Thanks I'll give the code a go. I am retrieving a result set from a webservice which returns a different set of columns (of differing datatypes) and rows each time. I don't know the names of the columns which is why I am trying to get the data however I can.
Depending on the datatype I will perform a different action (sum the amount, format it etc).
JSON-serialized objects contain a hierarchy, w/o any reference cycles, so it should be fairly straightforward to traverse, something like
function visit(JSONobj, f)
{
for (var key in JSONobj)
{
var value = JSONobj[key];
f(key,value);
if (value instanceof Object)
visit(value, f);
}
}
where f is a function that does something with keys and values. (of course you could just write a function to do this directly).
What exactly are you trying to find within the object?

Categories