Backbone POST and GET to different endpoints - backbone.js

I created a Backbone model with below attributes that saves to database schema A through the /api/books end point. Each row is a book owned by a user.
url: '/api/books'
defaults: {
'userId': 0,
'bookId': 0,
'bookCategory' : 'abc'
},
I have another database schema which contains count of books by userId which I want to fetch after each user input save. I am planning to fetch the data through another end point.
url: '/api/books/:userId'
defaults: {
'userId': 0,
'bookCount': 0,
},
Do I need to create two backbone models in order to save and get from two different end points?

Related

How MongoDB refs works internally?

I am really confused about
' How document of other collection is stored in a document?'.
Does ref just keep the address of the document and populate on demand in O(1) or it stores like a relational DB and search throughout the table to get the relevant document?
e.g We have two collections, User and Posts.
User {
_id: mongoId of User,
name: String,
post: reference of post id
}
Post {
_id: mongoId of Post,
title: String,
body: String
}
Now, Users stores Post in form of a reference. While fetching the document which is posted by a particular user, will it go through all the documents in post and fetch the one which is relevant to us or it just store the direct reference to that document and fetch in O(1).

Update unique compound indexes on an existing data set

Problem:
I'm trying to update a unique compound index on an existing data set and Mongo isn't updating the index.
Background:
In the database for our web app we have a unique compound index using a user's clubID and email. This means emails must be unique in regards to a user's clubID.
I'm in the process of updating this index to allow users to share emails. We added a new property on the user model called 'primaryAccountHolder'.
I want the new compound index to allow users with same clubID to share an email but only one user in the same club can have the field primaryAccountHolder set to true. I have this index working locally but the updating on our existing data set is unsuccessful.
I believe this is because we have existing entries in our DB that won't allow this index to be updated. So my question is:
how can I achieve updating a compound index that maintains uniqueness on an existing data set?
Below are the indexes I have created using Mongoose / Typescript. These work locally but not on our existing db.
Old Index:
UserSchema.index({ email: 1, clubID: 1 }, { unique: true })
// Won't allow a user of the same club to have the same email. This is the index on our DB.
New Index:
UserSchema.index({ email: 1, clubID: 1 }, { unique: true, partialFilterExpression: { email: { $exists: true }, primaryAccountHolder: { $eq: true } } })
// Will allow users to share an email but only one of them can have the primary account holder field set to true.
The new index uses a partial filter expression. This is the part that isn't created on the existing data set.
Thanks for the help!
Sam Gruse
You'll have to drop and recreate the index:
UserSchema.dropIndex({ email: 1, clubID: 1 })
And then recreate it:
UserSchema.createIndex(
{ email: 1, clubID: 1 },
{ unique: true,
partialFilterExpression:
{ email: { $exists: true },primaryAccountHolder: { $eq: true } }}
)
from MongoDB Documentation:
https://docs.mongodb.com/manual/tutorial/manage-indexes/#modify-an-index
MongoDB cannot update an existing index. You need to drop the current index and create the new one.
From https://docs.mongodb.com/manual/tutorial/manage-indexes/#modify-an-index:
To modify an existing index, you need to drop and recreate the index. The exception to this rule is TTL indexes, which can be modified via the collMod command in conjunction with the index collection flag.

Cannot update item where two columns combination must be unique

I am making an application with azure mobile services which stores users hours and points in a table called attended users. The problem I have is when I try and update the one specific user it selects all of the users with the same club id. I need an update function that finds a user with a club id and UniqueUserID that are unique, then updates the hours and points based on the one result.
Controller Code
$scope.saveChanges = function(){
$scope.show($ionicLoading);
var query = client.getTable('AttendedClubs').update({id: clubID.getJson(), UniqueUserID: memberID.getJson(), Hours: $scope.profile.Hours, Points: $scope.profile.Points}).done(function(results) {
$scope.hide($ionicLoading);
}, function(error) {
$scope.hide($ionicLoading);
alertDialogue.pop("No Internet Connection","Check Connection");
});
}
You are using a unique ID of the clubID. You need to construct this. When you create your object, do something like:
var table = client.getTable('AttendedClubs');
table.insert({
id: uuid.v4(),
clubID: clubID.getJson(),
UniqueUserID: memberID
...
});
To update all records, first do a fetch, then do an update on a per-record basis. Use the id to uniquely identify the record.

Is there a built-in function to get all unique values in an array field, across all records?

My schema looks like this:
var ArticleSchema = new Schema({
...
category: [{
type: String,
default: ['general']
}],
...
});
I want to parse through all records and find all unique values for this field across all records. This will be sent to the front-end via being called by service for look-ahead search on tagging articles.
We can iterate through every single record and run go through each array value and do a check, but this would be O(n2).
Is there an existing function or another way that has better performance?
You can use the distinct function to get the unique values across all category array fields of all documents:
Article.distinct('category', function(err, categories) {
// categories is an array of the unique category values
});
Put an index on category for best performance.

ServiceStack.OrmLite returning "empty records"

I´m starting with ServiceStack and using OrmLite to access my database. I used the Northwind example that comes bundled and modified it to access a SqlServer Database.
I changed the name of the table (Customer to Client) and the POCO class (Customer.cs) attributes so they match the correct ones in my table. When the request is made the returned data consist on a array containing N empty objects being N the number of records on the desired table.
If I add/remove records to the table this action is reflected on the returned data. So, OrmLite is querying the table but I can´t understand why my records are not populated.
The original json output:
{
Customers: [
{Id:"...", CompanyName:"...", },
{Id:"...", CompanyName:"...", },
{Id:"...", CompanyName:"...", }
],
ResponseStatus: {...}
}
After modification, I'm receiving:
{
Clients: [
{},
{},
{}
],
ResponseStatus: {}
}
Note the array with the N empty objects as value of the Clients key.

Resources