MongoDb: How to merge contents of two documents in mongoDB? - node.js

I allow user to skip login or login using FB. When user logs in I create user in User collection using FB profile ID and when he skips it I still create his user using some unique client ID. In the client app, user can bookmark articles and I sync it to server. User can login using FB at any time later. Once user logs in using FB I have to merge user created using FB and user created using Client ID so that his stored data maps perfectly.
What I have tried:
Consider user skipped login and I create a user in USER collection.
User logs in using FB in between life time of the app and I will search this FB ID in my collection. If this ID does not exist I update skipped user document with FB id. If it exist I will merge data from FB user document to skipped user document and delete FB user document.
Another method:
I don't sync any bookmark to the server until user logs in. Once he logs in I will sync all data at once.
What is the efficient way of doing this kind of operation?

If I understand correctly, you need to find the document with the same clientId, and add to it the facebook user document.
You can do this with mongo update command, using $set which merges the new document with the existing mongo document https://docs.mongodb.com/manual/reference/operator/update/set/
so your code would look like this
db.collection("users").update(
// first parameter is mongo query to match based on clientId
{ clientId: 100 },
// second parameter is the document that will be merged using $set
{ $set: facebookDocument })
The result of this will be the existing document merged with all the properties of facebookDocument

Related

How to hide logged in user from User list in Nodejs

When fetching a user list in my application it fetches the logged-in user list when filtered list also. I want to fetch only the users other than logged in user in my list.
I'm using mongodb and nodejs as backend.

Express.js: user access just the data created by them

Im struggling with Express.js and MongoDB.
I already configured passport login and passport-jwt, and it works just fine, but now I have a problem.
For example I have two users and one table, and I want when the first user login in the application, they have access the data created by them.
What is the best solution for this? Create one table per user?
Help me out and I appreciate your help!
you can update your schema of data that will be stored inside table, by adding:
owner {
type: mongoose.Schema.Types.ObjectId,
ref: 'User'
}
while user will be the name of modeling that created as users database, so every data inside that table will have id of its owner, and every time you want to display that data or send to the user, you can check if id of the owner of that data equals to the id of the user already login, and depend on that you can display the data of table or not.
and the id of the user will be stored inside req.user._id which will be created by passport package.

Scenarios definition for JWT/API Process with Cucumber/Behat

I have a scenario where basically:
- Authorized (JWT) user access my API
- If user exists, info get synched with DB, if not, gets created
- ETC ETC
My question is, how would I proceed creating this scenario? There's a lot (ok, 4) parameters that should be in the request, but I don't want to polute the scenario with information that can confuse a normal user reading the scenario.
This is what I have:
Scenario: Non Existent user access the API
Given an authorized user access the API
And user does not exist on API database
When user access the API
Then user details are added to API database
And user does exist on API database
A user accessing the api will have: email, auth0_id, nickname and name. Just not sure if I should code those info on the Scenario or somehow do it on the Context file.
Edit:
Can I have some "parameters" to be set IN the Context file, instead of in the .feature file? i.e. On the feature file I say "Non existent user access the application" and inside the Context file, in the function associated with this step, I make sure I create a user that does not exist on the database and so on? Would this be a good way of keeping thinks separated from the .feature scenarios?
Thanks
I would write it like this:
Scenario: API - new user access the API
Given I have a new user
When I access the API with the new user
Then the user is added to the API database
First step would generate the user details and save them in a variable, second would make the call to the api (using the saved variable and generate the JWT) and the last one will check the details in the api.
You can declare new as parameter like:
#Then /^I access the API with the (new|other_user) user$/
Anyway, you should declare it as simple as possible in a manner that has sense to you can that you can easily reuse.

After doing PullAsync() on a table, I am unable to update the table data - Offline data sync in .Net Mobile service

We are developing a windows store 8.1 app, In that we implemented Offline data sync using Azure mobile Service(.net backend).
We are using Mobile service with On-Premise SQL Server with existing database using Code first migrations.
We have a USER table which stores user emailId and offlinePin in the database.
We also implemented AAD single sign on in the app. For all active directory users those who are going to use the app, we added their email ids to the User table with out offlinePin value.
In client app, We are calling the following line based on the user who log in to the app using AAD single sign on.
Declaration:
private IMobileServiceSyncTable<Users> usertable= App.MobileService.GetSyncTable<Users>();
await usertable.PullAsync("SyncLoggedInUserInfo", usertable.Where(user => user.Email == App.UserEmail));
Now for the User who log in based on his/her mail Id we are pulling their information from USER table using above line.
If the logged in user don't have an offline pin then the app will prompt the user to create one and save that into the local SQLite USER table.
For updating the user offlinePin we are calling the following lines
var userInfo = await usertable.Where(x => x.Email == App.UserEmail).ToListAsync();
if (userInfo .FirstOrDefault()!=null)
{
var emp = userInfo .FirstOrDefault();
emp.OfflinePin = pinpswrdbx.Password;
await usertable.UpdateAsync(emp);
}
After updating done we are pushing those changes to the Server.
await App.MobileService.SyncContext.PushAsync();
Here the issue is, the line calling for update the offline pin is not working, means
await usertable.UpdateAsync(emp); is not updating the respective employee information in local table. We are not getting any exception here, it's executing successfully but the offline pin column value is not updating with the user entered pin value.
It is happening only for the rows of data added directly on database and synced to local SQLite DB which are not created within the app, if the record is inserted/created with in the app then that data is updating and am able to push those changes to server as well.
I have to use the existing database and tables which already having data in it and the app should be able to update the data and push the changes back to the server DB.
Can anybody help me where I am missing or doing wrong?
The problem is with the database, it is having case sensitive collation. I used fiddler to track what exactly the mobile service doing while pushing result to server, then I found that in the request URL all the stating letters of the table columns are automatically converting in to capital letters, but in database they are in small letters. May be due to this the data wasn't updated in database and roll backing in local DB as well.
To overcome this issue I had written the below line of code in DBContext class in mobile service project and did publish.
modelBuilder.Entity<User>().Property(p => p.Offlinepin).HasColumnName("offlinepin");
After this updating is working. I don't know why Mobile service did not show any error message when push failed instead it roll back the local values.

How can I upload a picture in couch db and retrieve it

i am working on Couch DB ,i want to create a user profile after user signs up for my website.
During registration,the user uploads his image and then it is added to that user's document in Couch Db and retrieved so that all his details along with his image are displayed as his profile.
You can store a picture as an attachment, either in the user's profile, or, in preference, to a separate database. Here's uploading a single attachment to a new document:
curl -vXPUT $COUCH/testy/doc/attached-jpg --data-binary ~/tmp/swirl/docs/test/data/m74.jpg
Obviously if you're updating an existing document you'll need to provide _rev fields appropriately.
Typically the user profile is kept lean and the profile images are handled from a separate db which then doesn't require caching that in RAM on the server if not needed. A validation function can be used to ensure that the only user who updates a profile picture is the owning user.

Resources