Cassandra data model to store embedded documents - mongodb

In mongodb we can able to store embedded documents into a collection.Then, How do we store embedded documents into cassandra??? For this sample JSON representation???
UserProfile = {
name: "user profile",
Dave Jones: {
email: {name: "email", value: "dave#email.com", timestamp: 125555555},
userName: {name: "userName", value: "Dave", timestamp: 125555555}
},
Paul Simon: {
email: {name: "email", value: "paul#email.com", timestamp: 125555555},
phone: {name: "phone", value: "4155551212", timestamp: 125555555},
userName: {name: "userName", value: "Paul", timestamp: 125555555}
}
}

If your document nesting level is not too deep, you can use User Defined Types from C* 2.1.
I personally suggest to rethink your schema into more flat form like:
create table profiles (
name text,
name2 text,
email text,
username text,
ts timestamp,
primary key (name,name2) // compound primary key!
)

Related

How to query JSON data from an object in sequelize?

I'm new in sequelize so I'd like to ask how to query some specific data from JSON, the JSON in question is inside an onject. Suppose I have this data structure:
{ id: INTEGER, name: json, email: STRING}
{ id: 1, name : { "first": "foo", "last": "bar"}, email: blabla#gmail.com}
And want to retrieve only id and only "last" from all data like this:
{{id: 1, name: {"last": "baz"}, {id: 2, name: {"last": "doe"}}
How can I do this with sequelize guys? I'm really lost. I use postgres as database by the way. Thanks.
You can use attributes option with sequelize.json
const payload = await sequelize.<modelname>.findAll({
attributes: ['id',
[sequelize.json('name.last'), 'last']]
});
It means like:
select id, name->>'last' as last from ...

Can I edit multiple documents from an array of them in Mongo?

well...
I have a collection of users, one user looks like
{ userId: "someId", fullName: "AnyName", email: "anyEmail#mail.com"}
and of course Mongo adds one _id
all fine here, but a have an express server and I receive and array of existing users with a POST.
[
{ userId: "123", fullName: "name1 EDITED", email: "anyEmail1#mail.com"},
{ userId: "124", fullName: "name2 EDITED", email: "anyEmail2#mail.com"},
{ userId: "125", fullName: "name3 EDITED", email: "anyEmail3#mail.com"},
{ userId: "126", fullName: "name4 EDITED", email: "anyEmail4#mail.com"}
]
I want edit the existing user with the news values receive in the array but I don't know if I have to iterate and update each document or if I can pass the array of users to update all of them with a single query.
similar to what "insertMany" method does

how to database design for user authentication like student, teacher , super admin

I am trying to create different types of registration for user . I have got three collection for users . I have been references user collection in both of teacher and student because I need to get email and password.
If a teacher register including email, password, firstname , lastname etc , there is a collection .
if a student register including email, password, firstname , lastname etc , there is another collection .
our all of email and password will be one collections
user - table/collection
- email : test#gmail.com
- password: asdfasdf
student - table /collection
- firstname: 'sujon"
teacher - table/collection
- firstname: "sujon"
const UserSchema = mongoose.Schema({
email: {
type: String,
required: true,
},
password: {
type: String,
required: true,
},
isAdmin: {
type: Boolean,
default: false,
},
})
const StudentSchema = mongoose.Schema({
user: {
type: Schema.Types.ObjectId,
ref: 'user',
},
firstname: {
type: String,
},
lastname: {
type: String,
},
photo: {
type: String,
},
education: {
type: String,
},
birth: {
type: Date,
},
sex: {
type: Boolean,
},
})
const TeacherSchema = mongoose.Schema({
user: {
type: mongoose.Schema.ObjectId,
ref: "user"
},
firstname: {
type: String
},
lastname: {
type: String
},
photo: {
type: String
},
designation: {
type: String
},
birth: {
type: Date
},
sex: {
type: Boolean
}
});
how can implement database design
Creating a single User schema would be fine. You can have a single schema with all properties (since all three types of user have almost same properties) and then add a 'roles' field like this:
roles: [{ type: String }]
The roles field can have multiple roles [ 'Teacher', 'Student' ... ]. In this way a single user can have multiple roles for example a Teacher can also be an admin etc.
Also you won't have to create a new model whenever a new role is introduced.
Users can be queried based on their roles. The roles can then be used for authentication as well for example a user with role 'Teacher' can create assignment or a user with role 'Student' can submit assignments. When registering a user you can set some sort of model validation on the api or client side to accept a certain model.

Mongodb Schema using mongoose

This is my user schema :
const userSchema = new Schema({
email: String,
username: String,
password: String,
secretToken: String,
active: Boolean,
type: String
}, {
timestamps: { // this will give us the detail when the account is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
this is my requirement Schema:
const requirementSchema = new Schema({
name: String,
age: String,
class: String,
subject: String,
email: String
}, {
timestamps: { // this will give us the detail when the requiremnt is created
createdAt: 'createdAt',
updatedAt: 'updatedAt'
}
});
how can I bring id from user schema to the requirement schema as secondary key?
I think mongodb is not meant for relationship. I could led to very bad practice if your database grow. But you can try https://docs.mongodb.com/manual/reference/database-references/#DatabaseReferences-DBRef for Database References.
There is no primary key foreign key concept in Mongodb. and normalization is also discouraged in mongodb.

Mongodb query select performing

I'm trying to get this result:
{
facebookId: [
"123123", "321321", "999999"
]
}
But i get this result:
{
\"mobileUser\": {
\"facebookId\": \"1547339005533880\"
}
},
{
\"mobileUser\": {
\"facebookId\": \"958277234198503\"
}
},
{
\"mobileUser\": {
\"facebookId\": \"10201611922458772\"
}
}
My model:
var roomModelSchema = mongoose.Schema({
roomId: { type: String, index: true },
mobileUser: {
socketId: String,
facebookId: { type: String, index: true },
email: String,
name: String,
photoUrl: String,
genderType: String,
birthday: Date,
city: String,
likeCount: Number,
insertDate: Date
},
insertDate: Date
})
My query is:
Room.find({ "roomId": req.query.roomId }).select("mobileUser.facebookId -_id").exec(function(err, users) {
});
I'm work on it to know how mongodb query works. Because if i use to build this query in sql it's very easy. I have difficulties to understand it.
If you think about MongoDb in terms of SQL RDBMS, each mongodb document corresponds to row in SQL DB.
So for example if you execute in SQL
select * from myTable
you will get
facebookID | name | city
--------------------------------
13453 | A | A
2213 | B | B
312321 | C | C
and if you do the simialr query in mongo
db.myTable.find({})
you will get
{facebookId: 13454, name: A, city: A},
{facebookId: 2213, name: B, city: B},
{facebookId: 312321, name: C, city: c},
So now if you query for one field and execute in SQL
select facebookId from myTable
you will get
facebookID
----------
13453
2213
312321
And in mongoDb, if you execute
db.myTable.find({}, {id:false, faceBookId: true})
{id:false, faceBookId: true} - correspond to select methods from Mongoose. It specifies what fields should be included in documents
you will get
{facebookId: 13454},
{facebookId: 2213},
{facebookId: 312321},
If you would like to get these fields from multiple documents and output single document with facebookId field that contains all values, you can use aggregate framework
db.myTable.aggregate(
{$group:{_id:null, facebookId:{$addToSet:'$Name'}}}
)
This will return:
{
_id: null,
facebookId: [
"13454", "2213", "312321"
]
}