Schema for User table with multi strategy sigup - javascript

While working on one project faced with problem of storing user information for different passport strategies(local, facebook, twitter).
At the begining my UserSchema had such look:
User = mongoose.Schema(
{
"email" : { type : String , lowercase: true , trim : true , unique : true } ,
"providerId" : { type : String } ,
"providerName" : { type : String } ,
"hashedPassword" : { type : String } ,
"salt" : { type : String } ,
"strategy" : { type : String } ,
"created" : { type : Date , default : new Date().valueOf() } ,
"lastConnected" : { type : Date , default : new Date().valueOf() } ,
"accessToken" : { type : String } ,
"securityToken" : { type : String , default : "" } ,
"roles" : [ { type : String } ]
}
);
But unique email brings problem with emails because two users with twitter stategy will have null email and this leads to error.
I thought about not making email unique but this brings alot( from the first look ) of problems.
One the solutions I see is making different schemas for each strategy but this is very difficalt to maintain.
Is there some other way of solving this issue. What are the best practices?
P.S. I swear I googled but didn't find any solution

It seems like you want to be able to say unique:true and specify allowNulls. This now seems possible, as you can see here: https://stackoverflow.com/a/9693138/1935918

Related

import excel file and read it using ionic

i want to upload a excel file and read it using ionic-angular , i try with this code but it doesn't work for ionic app its just work for browser i dont know why ?
i want to read it and get a value from it and it doesn't work in ionic app (android )
if there is any plugin for excel file help me
this is my code
<ion-input #inputFile id="input-file" type="file" (change)="excelRead($event)" multiple></ion-input>
public excelRead(e :any ){
let fileReaded :any ;
fileReaded = e.target.files[0];
let type = e.target.files[0].name.split('.').pop();
console.log(fileReaded.name)
const schema = {
'Material' :{
prop : 'material',
type : String ,
required : false
} ,
'Description' :{
prop : 'description',
type : String ,
required : false
} ,
'Emplacement' :{
prop : 'emplacement',
type : String ,
required : false
} ,
'Physique' :{
prop : 'physique',
type : Number ,
required : false
} ,
'Sap' :{
prop : 'sap',
type : Number ,
required : false
} ,
'Ecart' :{
prop : 'ecart',
type : Number ,
required : false
} ,
'Cagette' :{
prop : 'cagette',
type : String ,
required : false
} ,
};
readXlsxFile(fileReaded, {schema}).then((data) =>{
console.log("message1")
if( data.rows){
for (let i of data.rows){
console.log("message2")
this.inventaireTab.push(i);
}
}
console.log("message3")
console.log(this.inventaireTab)
for (let i = 0; i < this.inventaireTab.length; i++) {
this.quantite.push(this.inventaireTab[i].physique)
}
console.log(this.quantite)
}
)
}
if you have any suggestion help me
You have to use Ionic File Opener Plugin:
https://ionicframework.com/docs/native/file-opener
Use this link for reference as in Plugin Documentation usage you'll find only for PDF: how to open (doc, ppt, xlsx, pdf, jpg ,png) file using ionic native file opener
By this you can open different file formats.

Mongo script to update an object if its a certain value

I am trying to update a document in Mongo if it has a certain value within a field.
{
"_id" : ObjectId("myObject"),
"source" : "BW",
"sourceTableName" : "myTable",
"tableName" : "tier",
"type" : "main",
"fieldMappings" : [
{
"sourceField" : "tier_id", <~~~~ If this is "tier_id", I want to change it to "trmls_id"
"reportingField" : "bw_id",
"type" : "integer",
"defaultMap" : {
"shouldErrorOnConvert" : false
}
}
]
}
I tried something like
db.getCollection('entityMap').update({"sourceTableName":"myTable"}, {"fieldMappings.0.sourceField":"trmls_id":{$exists : true}}, { $set: { "fieldMappings.0.sourceField": "tier_id" } })
and I think it is failing on the $exists parameter I am setting.
Is there a more cleaner/dynamic way to do this?
The whole query I am trying to formulate is
In the table myTable
I want to check if there is a sourceField that has the value tier_id
If there is tier_id, then change it to trmls_id
Otherwise do nothing
If there is a similar StackOverflow question that answers this, I am happy to explore it! Thanks!
There is a syntax mistake and you can use positional operator $ in update part because you have already selector field in query part,
db.getCollection('entityMap').update(
{
"sourceTableName": "myTable",
"fieldMappings.sourceField": "tier_id" // if this field found then it will go to update part
},
{
$set: {
"fieldMappings.$.sourceField": "trmls_id"
}
}
)

Mongo search for value of key in nested array [duplicate]

This question already has answers here:
How to query nested objects?
(3 answers)
Closed 6 years ago.
Here's my document:
"_id" : "dAWcFHJzDPJ2XT9Sh",
"createdAt" : ISODate("2016-04-22T18:03:47.761Z"),
"services" : {
"password" : {
"bcrypt" : "$2a$10$NYf53o/Uu8PvHPsGllRGA.WLbVpspNM4jk/6FtCzZLW.70.uQ2HXe"
},
"resume" : {
"loginTokens" : [
{
"when" : ISODate("2016-04-22T18:03:47.771Z"),
"hashedToken" : "dECxxuV/QyU2AU+/Zcrqc2Ftq64ZTrdHj5mN/rTGrxU="
}
]
}
},
"emails" : [
{
"address" : "Adammoisa#gmail.com",
"verified" : false
}
],
"profile" : {
"first_name" : "Adam",
"last_name" : "Moisa"
}
I want to search for an email in emails[i]address
Here's what I've tried (I'm using Meteor; Meteor.users.find({}).fetch() returns all users in database as objects formatted like above):
Meteor.users.find({"emails[0]address": "Adam"}).fetch();
I want that to return the above object as "Adam" is an email in emails[0]address
Thanks!
No need to specify the index.
Meteor.users.find({"emails.address": "Adam"}).fetch();
The index doesn't need to be specified, so you can do this:
Meteor.users.find({"emails.address": "Adam"}).fetch();
However if you do want to use a specific index, do this:
Meteor.users.find({"emails[0].address": "Adam"}).fetch();
Figured it out:
Meteor.users.find({"emails.address": "Adammoisa#gmail.com"}).fetch();
You can just do emails.address and it will match any address key with your value from any array in emails.
(Used regex from searchSource to make it work with anything that matches:
function buildRegExp(searchText) {
// this is a dumb implementation
var parts = searchText.trim().split(/[ \-\:]+/);
return new RegExp("(" + parts.join('|') + ")", "ig");
}

Diff and exchange JSON value between default and actual response

I have 2 JSON object default and actual, they can be NESTED. I want to write an javascript algorithm to compare and exchange the value between default and actual. But I am stuck and only able to process the outermost level of the JSON
Here are the example:
Default:
{
username : "string",
phone : null,
school : "string",
GPA : {
major : null,
minor : null
}
}
Actual:
{
username : "David",
phone : 12345,
school : "Harvard",
password : "david#harvard"
GPA : {
major : 3.9
}
}
after the diff and exchange value the result should be:
{
username : "David",
phone : 12345,
school : "Harvard",
GPA : {
major : 3.9,
mimor : null
}
}
The idea is have the default model, if the response have extra property such as password, the algorithm should remove such property.
On the other hand, if the response doesn't have the require property, the algorithm should fill in the default value.
So the final result should only contain the keys that define in the default model, and the value will come from the response, if a key does not exist use the default key value pair.
If you can use jQuery, then $.extend() can be used to accomplish this.
var def = {
username : "string",
phone : null,
school : "string",
GPA : {
major : null,
minor : null
}
}
var actual = {
username : "David",
phone : 12345,
school : "Harvard",
password : "david#harvard",
GPA : {
major : 3.9
}
}
var extended = $.extend(true, {}, def, actual);
Here's a jsfiddle that should demonstrate this.
https://jsfiddle.net/38L808y1/

MongoDB - Updated $ref value unable to query new value

I've posted the following question which has been answered correctly:
MongoDB - Updating only $ref from DBRef field type
Despite of this when I execute the find method like this:
{ "codeId" : { "$ref" : "code" , "$id" : { "$oid" :
"4ff1c08c6ef25616ce21c4b6"}} }
The document isn't returned... Any idea why?
After the update the document is stored like this:
{ "_id" : { "$oid" : "5097ae1cd3159eb52d05574c"} , "codeId" : { "$ref"
: "code" , "$id" : { "$oid" : "4ff1c08c6ef25616ce21c4b6"}} }
By the way, using uMongo GUI, if I select the Update option over this stored document, and save it, without making any changes whatsoever, and then make the find query once again, the document is returned by the query...
Thanks
This is a clearly one of those DBRef "tweaky" things...
As a temporary (but probably correct) fix, I managed to solve this problem executing this javascript procedure:
var cursor = db.menu.find( { "codeId.$ref" : "version" } );
while( cursor.hasNext() )
{
var document = cursor.next();
db.menu.update(
document,
{ $set: {"codeId" : DBRef("code", document.codeId.$id) }},
{ upsert: false, multi: true }
);
}
Still, I won't consider this to be the best way to achieve what I want... Any other solution that involves less lines?

Categories