I have three successives calls to the same web service to post some comments.
The structure is : {Doc_No is TEXT, Line_No is NUMBER, Description is TEXT}
Comments_doc = [{
Doc_No: 'xx_01',
Description : 'TOTO_1'
},
{
Doc_No: 'xx_01',
Description : 'TOTO_2'
},
{
Doc_No: 'xx_01',
Description : 'TOTO_3'
}]
$.each(Comments_doc, function(aComment){
$.post(...) //here post aComment
});
The primary keys are : Doc_No and Line_No
Line_No is calculated by the web service, it looks for the last one and add one.
The problem is ; looking for the last record is faster than the insert, so at any given time, I have the same Line_No for two differents records !
How could I avoid this ?
thank you for your help.
Related
We have the records in the below format :
[
{
id: 1,
name : 'Chris',
class : 'x',
year : 3,
},
{
id: 2,
name : 'John',
class : 'y',
year : 4,
}
]
I want to fetch multiple records based on id with a single api call.
This could be achieved either by passing multiple ids in a get call or using a post call to post the list of ids.
Get call with multiple ids
GET https://api.com/school/names?id=1,2,3
Post with multiple ids
POST https://api.com/school/names?action=get
request body :
{
id1: 1,
id2: 2,
id3: 3
}
We don't have issue with the url length as we will not cross the 2048 characters limit.
I see that many public apis use both of the above approaches, I have a feeling that using a POST call to fetch data is against the REST principle.
Could some one suggest what is the best approach for this scenario and the advantage of the approach compared to the other one.
I just use mongoose recently and a bit confused how to sort and paginate it.
let say I make some project like twitter and I had 3 schema. first is user second is post and third is post_detail. user schema contains data that user had, post is more like fb status or twitter tweet that we can reply it, post_detail is like the replies of the post
user
var userSchema = mongoose.Schema({
username: {
type: String
},
full_name: {
type: String
},
age: {
type: Number
}
});
post
var postDetailSchema = mongoose.Schema({
message: {
type: String
},
created_by: {
type: String
}
total_reply: {
type: Number
}
});
post_detail
var postDetailSchema = mongoose.Schema({
post_id: {
type: String
}
message: {
type: String
},
created_by: {
type: String
}
});
the relation is user._id = post.created_by, user._id = post_detail.created_by, post_detail.post_id = post._id
say user A make 1 post and 1000 other users comment on that posts, how can we sort the comment by the username of user? user can change the data(full_name, age in this case) so I cant put the data on the post_detail because the data can change dynamically or I just put it on the post_detail and if user change data I just change the post_detail too? but if I do that I need to change many rows because if the same users comment 100 posts then that data need to be changed too.
the problem is how to sort it, I think if I can sort it I can paginate it too. or in this case I should just use rdbms instead of nosql?
thanks anyway, really appreciate the help and guidance :))
Welcome to MongoDB.
If you want to do it in the way you describe, just don't go for Mongo.
You are designing the schema based on relations and not in documents.
Your design requires to do joins and this does not work well in mongo because there is not an easy/fast way of doing this.
First, I would not create a separate entity for the post details but embedded in the Post document the post details as a list.
Regarding your question:
or I just put it on the post_detail and if user change data I just
change the post_detail too?
Yes, that is what you should do. If you want to be able to sort the documents by the userName you should denormalize it and include in the post_details.
If I had to design the schema, it would be something like this:
{
"message": "blabl",
"authorId" : "userId12",
"total_reply" : 100,
"replies" : [
{
"message" : "okk",
"authorId" : "66234",
"authorName" : "Alberto Rodriguez"
},
{
"message" : "test",
"authorId" : "1231",
"authorName" : "Fina Lopez"
}
]
}
With this schema and using the aggregation framework, you can sort the comments by username.
If you don't like this approach, I rather would go for an RDBMS as you mentioned.
I have been trying to develop an app that takes an order on Shopify on a different channel. I successfully placed an order through the API but I am not able to include the discount code along with the order. The JSON object for the POST data is as below:
{
order: {
email : request.params.order.email, // string
financial_status : 'pending', // string
send_receipt : true, // boolean
send_fulfillment_receipt : false, // boolean
note : request.params.order.note, // string
discount_codes : [], // supposed to be an array of Object| Problem here,
line_items : request.params.order.line_items, // array
customer : request.params.customer, // JSON object
billing_address : request.params.order.billing_address, // JSON object
shipping_address : request.params.order.shipping_address // JSON object
}
}
According to the documentation, the discount_codes is like this -
Applicable discount codes that can be applied to the order. If no codes exist the value will default to blank. A Discount code will include the following fields:
amount: The amount of the discount.
code: The discount code.
type: The type of discount. Can be one of : "percentage", "shipping", "fixed_amount" (default).
What am I doing wrong? My discount_codes is this
[{amount: 100,code:'WELCOME10',type:'percentage'}]
Has anyone done this before?
According to this response from Shopify what you are trying to do is only possible if you pass the total_discounts field along as well with the total amount of the discount you want to apply.
As you will see in this other answer, any codes you have created through Shopify are not available to use with the API and their usage will not be recorded.
I was trying to use this API in order to test the application of different coupon codes that I was generating, but this does not seem to be possible. Apparently, the API was intended for applying discounts that are custom, not ones that already exist in Shopify. This is a frustrating limitation to me.
I successfully create orders with discounts all the time, without ShopifyPlus as that is irrelevant. The data structure that works for me looks like this:
[ { "code": "Shop By PizzleFuzzle 10%", amount: "10", "type": "percentage" } ]
The discount object is available only for Shopify Plus merchants.
Once you are a Shopify Plus merchant, you will be able to create discount codes like that:
POST /admin/discounts.json
{
"discount": {
"discount_type": "percentage",
"value": "15.0",
"code": "balderdash"
}
}
Please see more detailed documentation in the discount object at Shopify API: https://help.shopify.com/api/reference/discount
You should use the value property name instead of amount property name.
e.g.
{value: 100,code:'WELCOME10',type:'percentage'}
and not
{amount: 100,code:'WELCOME10',type:'percentage'}
I'm looking for advice on my approach here- I want to be sure I'm doing things in the "meteor way" and keeping the code fast.
Current situation:
We have a collection for Questions. Each question has a nested collection of Answers. Through a REST API, a device relays the answers that were selected by users.
Based on the answers that were selected, we show a chart for each question- simple number breakdowns and percentage bars. To improve performance, we've been tracking the number of responses each Answer has received on the Answer itself.
The publication looks (basically) like this:
Meteor.publish('questionsBySiteID', function(site_id){
return Questions.find({site_id: site_id});
});
And the route like this:
Router.route('/sites/:_id/questions', {
name: 'questionsList',
waitOn: function(){
return [
Meteor.subscribe('questionsBySiteID', this.params._id),
];
},
data: function(){
return {
publishedQuestions: Questions.find(
{ site_id : this.params._id, active: true, deleted: {$ne: true} },
{ sort : { order: 1} }
),
archivedQuestions : Questions.find(
{ site_id : this.params._id, active: false, deleted: {$ne: true} },
{ sort : { updated_at: -1 } }
),
deletedQuestions : Questions.find(
{ site_id : this.params._id, deleted: true },
{ sort : { updated_at: -1 } }
)
};
}
});
Change required:
Now we want responses to be date-filterable. This means the denormalized response counts we've tracked on Answers aren't very useful. We've been tracking another collection (Responses) with more a "raw" version of the data. A Response object tracks the module (questions in this case), question_id, answer_id, timestamp, id for the customer the question belongs to, etc.
Question:
Is this something that template subscriptions help with? Perhaps we need a publication that accepts a question_id and optional start/end dates for the filter. The stats template for each question would be subscribed to applicable Responses data in Template.question.create(). Based on the question_id, the publication would need to find Responses for related answers within the date filter. And maybe we use the publish-counts package to count the number of times each answer was selected and publish those counts.
The Responses collection will be quite large, so I'm trying to be careful about what I publish here. I don't want to waitOn all Responses to be published.
I wanna display the fields names of my records dynamically :
Example:
if i have this three records
{
"_id": {
"$oid": "blahblah"
},
"firstName": "Jon",
"lastName": "Doe",
"age" : "55"
}
{
"_id": {
"$oid": "blahblahblah"
},
"firstName": "Johnny",
"lastName": "Doedoe",
"weight" : "555lb"
}
What i want to display in my web page with angularjs is :
firstname : Jon
lastname : Doe
age : 55
firstname : Johnny
lastname : Doedoe
weight : 555lb
I know how to get specific values, for example to get the firstname i can do this : $scope.person.firstname (this is of course an example), but how can i get the label "firstname". How can i ask angularjs to get all the labels ?
Thanks
You'll have to write a query to your DB to get these records, depending on your server that query will look different. The below is a node version of the call
function getNames() {
db.collection.find().toArray(function(data, err) {
if (err)
//do something
else
res.send(data);
});
}
Now, from Angular, make a GET call to your server:
$http.get(url).success(function(data) {
$scope.names = data;
});
Now you have $scope.names set to your Array of data from the serv. Use an ng-repeat to display it all:
<div ng-repeat="name in names">
<span ng-repeat="(key, val) in name">{{key}}:{{val}}</span>
</div>
And that's it (well, basically, undoubtedly you'll have to tweak this).
Not sure if you're using Express and Jade with Angular, but alternatively if you don't want to make a separate http request (depends on how your app is configured) you could write an angular service that can be reused with any controller.
The purpose of the service would be to grab json and put it into your current scope so your controller can use it. Also you can inject this service dependency on every page (i.e. in every controller) and it'll load your documents for you.
Blog post and full repo here.