define an array of numbers inside unique key - arrays

I wish I could define a GET request by a resource like /amounts?id=1,2,3,4 where each id should be a number
However I am able to do only a resource like /amounts?id=1&id=2&id=3&id=4 by this code:
/amounts:
get:
description: Get Amounts
queryParameters:
id:
type: array
example: [1,2,3,4]
The other way I could make was by defining the id type as string and split it in my implementation, and the resource should be /amounts?id=1%2C2%2C3%2C4
/amounts:
get:
description: Get Amounts
queryParameters:
id:
type: string
example: "1,2,3,4"
Cant I define somehow the request to accept only an array of numbers, not repeating the key?

Related

mongodb find where columns of arrays don't match

i am looking for a possible solution to find results from a mongodb collection where two columns of arrays don't hold the same entries.
Something like this:
Message DB structure
{
id: ObjectId,
title: String,
body: String,
recipients: [{ObjectId, ref: 'User'}],
visualized_by: [{ObjectId, ref: 'User'}]
}
when a User visualizes the message his id gets stored in the visualized_by column
Now, from an admin panel I want to visualize only the messages that were not seen by all the recipients like this:
Message.find({recipients: {$each: {$nin: "$visualized_by"}}})
OR
Message.find({$where: {recipients: {$each: {$nin: this.visualized_by}}}})
But it does not work of course.
Any help will be appreciated.
Thanks!

Is there any downside to flatten data model schema in NOSQL Mongodb?

I'm using NodeJS and typescript and I'm making typing for my functions in order to get auto-completion.
Currently I have a schema like this for User entity
interface User {
"_id": string
"coach": {
"businessCard": string
}
"firstname": string
}
I'm using a nested object for the coach property, but my question is : What's the point of doing this, behind the scene MongoDB create another object which is referenced in a 1-to-1 relationship.
This probably reduce performances
I'm having issue to type my function to restrict fields that can be targeted in typescript
Let's see with an example :
await this.client.collection<User>('User').findOneAndUpdate(
{ _id: new ObjectID(userId) },
{"coach.businessCard": "50"},
);
will update the nested object.
But this syntax would have worked the same way using
interface User {
"_id": string
"coach.businessCard": string
"firstname": string
}
The difference is that now I can uniformly use one syntax which match the dot notation.
So what could be the advantage of using the first notation I gave compared to the second one which is flatten ?
Best regards
Advantages of using nested Document One-To-One-Relationship are as below:
1. It represents the connection between data fields. For example: to save the user address we need to take multiple fields like:
-> Address line 1
-> Address line 2
-> Locality
-> Region
-> Postcode
-> Building
-> Sub-Building
-> etc.
With the One-To-One-Relation we can bind the related information in a single unit as below:
address: {
Address line 1: String ,
Address line 2: String,
Locality: String,
Region: String,
Postcode: Number,
Building: Number,
Sub-Building: Number,
etc: String
}
2. It is helpful while retrieving such information from the database. For example:
-> To fetch full address details I can project with a single key.
db.collection.find({},{ address: 1} )
-> To fetch specific address field I can project on that as well like below:
db.collection.find({},{ 'address.Locality': 1, 'address.Region': 1} )

How to order a dictionary by value in AngularJS

I've a REST api that returns the list of locales as dictionary:
{
"en-uk": "English UK",
"en-us": "English USA",
...
}
This dictionary is correctly ordered alphabetically by value.
When AngularJS receives it via HTTP, the dictionary gets automatically re-sorted by key, so when I bind to a select element the list of options is ordered by key, and the alphabetical order by key doesn't match the one by value, I get a wrong sorting.
The problem I suppose is due to the fact that such dictionary becomes basically one object with 800+ properties. How do I sort it by value?
First: You have to find all keys.
Second: Iterate all the keys.
Third: Then sort the array with values.
Please use the following:
let obj = {
"en-us": "English USA",
"en-uk": "English UK"
};
// Get an array of the keys:
let keys = Object.keys(obj);
// Then sort by using the keys to lookup the values in the original object:
keys.sort(function(a, b) { return obj[a] > obj[b] });
console.log(keys);
console.log(obj[keys[0]]);
You can modify the way you send the response from the server. Instead of sending the response as an object, send the stringified object.
The problem is indeed you cannot sort the values of the properties of an object. So I convert it to an array before binding it:
So,
languageResource.getCultures().then(function(cultures) {
vm.availableCultures = cultures;
});
becomes
languageResource.getCultures().then(function (culturesDictionary) {
var cultures = [];
angular.forEach(culturesDictionary, function (value, key) {
cultures.push({
lcid: key,
name: value
});
});
vm.availableCultures = cultures;
});
Seen this when the key is numerical. If the key's data type is string than it would keep its sorted state after an API call. If the key's data type is numerical, than, you would need set the key's value as a string and even add single quotes before and after the key's value, before the API sends it back.
I haven't tried the approach to stringfy the dictionary in the API. After the call you would parse the string back to an object with something like JSON.parse(string) might be your best bet.

MongoDB: Query and retrieve objects inside embedded array?

Let's say I have the following document schema in a collection called 'users':
{
name: 'John',
items: [ {}, {}, {}, ... ]
}
The 'items' array contains objects in the following format:
{
item_id: "1234",
name: "some item"
}
Each user can have multiple items embedded in the 'items' array.
Now, I want to be able to fetch an item by an item_id for a given user.
For example, I want to get the item with id "1234" that belong to the user with name "John".
Can I do this with mongoDB? I'd like to utilize its powerful array indexing, but I'm not sure if you can run queries on embedded arrays and return objects from the array instead of the document that contains it.
I know I can fetch users that have a certain item using {users.items.item_id: "1234"}. But I want to fetch the actual item from the array, not the user.
Alternatively, is there maybe a better way to organize this data so that I can easily get what I want? I'm still fairly new to mongodb.
Thanks for any help or advice you can provide.
The question is old, but the response has changed since the time. With MongoDB >= 2.2, you can do :
db.users.find( { name: "John"}, { items: { $elemMatch: { item_id: "1234" } } })
You will have :
{
name: "John",
items:
[
{
item_id: "1234",
name: "some item"
}
]
}
See Documentation of $elemMatch
There are a couple of things to note about this:
1) I find that the hardest thing for folks learning MongoDB is UN-learning the relational thinking that they're used to. Your data model looks to be the right one.
2) Normally, what you do with MongoDB is return the entire document into the client program, and then search for the portion of the document that you want on the client side using your client programming language.
In your example, you'd fetch the entire 'user' document and then iterate through the 'items[]' array on the client side.
3) If you want to return just the 'items[]' array, you can do so by using the 'Field Selection' syntax. See http://www.mongodb.org/display/DOCS/Querying#Querying-FieldSelection for details. Unfortunately, it will return the entire 'items[]' array, and not just one element of the array.
4) There is an existing Jira ticket to add this functionality: it is https://jira.mongodb.org/browse/SERVER-828 SERVER-828. It looks like it's been added to the latest 2.1 (development) branch: that means it will be available for production use when release 2.2 ships.
If this is an embedded array, then you can't retrieve its elements directly. The retrieved document will have form of a user (root document), although not all fields may be filled (depending on your query).
If you want to retrieve just that element, then you have to store it as a separate document in a separate collection. It will have one additional field, user_id (can be part of _id). Then it's trivial to do what you want.
A sample document might look like this:
{
_id: {user_id: ObjectId, item_id: "1234"},
name: "some item"
}
Note that this structure ensures uniqueness of item_id per user (I'm not sure you want this or not).

Griffon checkbox binding won't work

i am trying the following griffon code
on model:
#Bindable boolean hello1=false
on view:
checkBox(id:1,text: 'hello1', constraints:'wrap',selected:bind(target: model, targetProperty:'hello1'))
but it does say
ERROR org.codehaus.griffon.runtime.builder.UberBuilder - An error occurred while building test.TestView#1132e76
groovy.lang.MissingMethodException: No signature of method: java.lang.Object.setVariable() is applicable for argument types: (java.util.Collections$EmptyMap, java.util.Arrays$ArrayList) values: [[:], [1, javax.swing.JCheckBox[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.5,border=javax.swing.plaf.synth.SynthBorder#b101cf,flags=288,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,disabledSelectedIcon=,margin=javax.swing.plaf.InsetsUIResource[top=0,left=0,bottom=0,right=0],paintBorder=false,paintFocus=true,pressedIcon=,rolloverEnabled=true,rolloverIcon=,rolloverSelectedIcon=,selectedIcon=,text=]]]8-mar-2012 12.03.41 groovy.util.FactoryBuilderSupport createNode
AVVERTENZA: Could not find match for name 'setVariable'
i dont get what's the deal, i copied that from working examples on internet....
Use a String instead of a number for the value of the id: property, like this
checkBox(id: 'c1', ...)

Resources