Adding column in meanjs database - mongodb

the below code is the default structure of a meanjs.org database. How can I add a new column like Balance in the mongodb?
{
"_id" : ObjectId("597695d922fa1025a453ed39"),
"salt" : "aGHDfk3+7Bg1lR8YI2JTAg==",
"displayName" : "sample samples",
"provider" : "local",
"username" : "admins",
"created" : ISODate("2017-07-25T00:50:33.800Z"),
"roles" : [
"user"
],
"profileImageURL" : "modules/users/client/img/profile/default.png",
"password" : "McRqjKUIzslHzCrg8VLyXXQZEZS/GqQyBewvI8xgutppYGvsexcbqf/ua9foennx6xy0unRiHrVAB7T1jF2v1Q==",
"email" : "admin#admin.com",
"lastName" : "samples",
"firstName" : "sample",
"__v" : 0 }enter code here

The code you showed is an example of a user saved in your MongoDB database inside User collection.
To edit the User schema in MEAN.JS you can do so by editing the file located in /modules/users/server/models/user.server.model.js and add the properties you need.

Related

Best way to create index for MongoDB

I am having records stored in mongo-db collection for customer and there transactions with below format:
{
"_id" : ObjectId("59b6992a0b54c9c4a5434088"),
"Results" : {
"id" : "2139623696",
"member_joined_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"account_activation_date" : ISODate("2010-07-07T00:00:00.000+0000"),
"family_name" : "XYZ",
"given_name" : "KOKI HOI",
"gender" : "Female",
"dob" : ISODate("1967-07-20T00:00:00.000+0000"),
"preflanguage" : "en-GB",
"title" : "MR",
"contact_no" : "60193551626",
"email" : "abc123#xmail.com",
"street1" : "address line 1",
"street2" : "address line 2",
"street3" : "address line 3",
"zipcd" : "123456",
"city" : "xyz",
"countrycd" : "Malaysia",
"Transaction" : [
{
"txncd" : "411",
"txndate" : ISODate("2017-08-02 00:00:00.000000"),
"prcs_date" : ISODate("2017-08-02 00:00:00.000000"),
"txn_descp" : "Some MALL : SHOP & FLY FREE",
"merchant_id" : "6587867dsfd",
"orig_pts" : "0.00000",
"text" : "Some text"
}
]
}
I want to create index on fields "txn_descp", "txndate", "member_joined_date", "gender", "dob" for faster access. Can some one help me in creating index for this document? Will appreciate any kind of help and suggestions.
While creating the index there are a few things to keep in mind.
Always create the index for the queries you use.
Go for compound indexes whenever possible.
First field in the index should be the one with the minimum possible values.Ie, if there is an index with gender and DOB as keys, It is better to have {gender:1,dob:1}

Getting data range from firebase with swift

I would like to ask that question. The question is how to get specific data range from firebase ?
I have table on firebase like this:
"users" : {
"Jz3IpatRWiWoDbiYM62q6qbHB503" : {
"email" : "Kaanozdemir#gmail.com",
"lastName" : "Ozdemir",
"name" : "Kaan"
},
"PmeYYFiac0c55fU2sFpnTP308mC3" : {
"email" : "kevinhart#gmail.com",
"lastName" : "Hart",
"name" : "Kevin"
},
"r0bMqSGCWihFi2EF4u6ckSzLP8v1" : {
"email" : "Marcusalvarez#gmail.com",
"lastName" : "Alvarez",
"name" : "Marcus"
},
"A3tmSSGCWihFi2EF4u6ckSzLP8c1" : {
"email" : "taylorswift#gmail.com",
"lastName" : "Swift",
"name" : "Taylor"
},
"3SUTsiGCWihFi2EF4u6ckSzLP8v2" : {
"email" : "jimmyfellon#gmail.com",
"lastName" : "Fellon",
"name" : "Jimmy"
},
"lgSit3GCWihFi2EF4u6ckSzLP8u3" : {
"email" : "jaxteller#gmail.com",
"lastName" : "Teller",
"name" : "Jax"
}
For example, I would like to get users values between 2 and 4 [2 - 4](Marcus Alvarez - Taylor Swift - Jimmy Fellon).
Is there any way to do that server side ? I don't wanna get all data and pick values that I want. Anyone knows?
Change your JSON DB structure to include an index in every node :
"users" : {
"autoID1" : {
"email" :.....,
"lastName" : ......,
"name" :.......,
"index" : //e.g.. 1,2,3,4......
}
},
"noOfUsers" : 223,
If you are appending this users node via app, you have too keep track of the no of users in Database node users and keep updating the noOfUsers whenever a new user is added. And to set the next ones index number , just retrieve that node value i.e 223 and sees it and then increment the noOfUsers......
To retrieve between 2-4 .. Now you can use :
Database.database().reference().child("users").queryOrdered(byChild: "index").queryStarting(atValue: "2").queryEnding(atValue: "4").observe....

Check if particular field exists in mongoDb Collection,excluding one record

I have collection, as shown below
{
"_id" : ObjectId("58fe3768f997ca09d551c34e"),
"firstName" : "arbar",
"lastName" : "ame",
"email" : "test41#gmail.com",
"phone" : "9966589965",
"password" : "$2a$10$pgRWb8Db385A5BbicEDJ2erHuUQsAIVmjqVuccXj7x.1iptdY/z7a",
"team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"),
"activated" : false,
"accessLevel" : NumberInt(6)
}
{
"_id" : ObjectId("58fe37c2f997ca09d551c350"),
"firstName" : "Abrar Ahmed",
"lastName" : "asdf",
"email" : "test42#gmail.com",
"phone" : "9966158845",
"password" : "$2a$10$y3hPjuHeq0HVyukTnGCRT.k5xfSUH0z/mdGR8n7Gu09f7A7Z20bV6",
"team_id" : ObjectId("58ef6d0a11c37915acaf7c9b"),
"activated" : false,
"accessLevel" : NumberInt(6)
}
.
.
.
.
.
.
I am trying to check if email test41#gmail.com exists in this collection, if I use this.collection.findOne({ email: 'test41#gmail.com' }); will look for all the records in a collection but how would I exclude one record in a collection by using _id field as reference to exclude.
Here the email is exists in one record of the collection with
"_id" : ObjectId("58fe3768f997ca09d551c34e"), , but I want to exclude this record and search in rest of the records in a Collection.
Don't use findOne, use find to find all occurrences. Now if you want to exclude some document you can do by following
db.collection.find({$and: [{"email": "test41#gmail.com"}}, {"_id": {$ne: ObjectId("58fe3768f997ca09d551c34e")}}]})

How to Store Images into Specified Collections in MongoDB

I know it is possible to save binary files into using MongoDB using the "mongofiles" utility.
Is there any way to store Images into a specified collection, not in db.fs.files?
I want to create an users collection which is using to store user details along with their profile pic like this:
{
"_id" : "USR_1",
"username" : "willsmith123",
"firstname" : "will",
"lastname" : "smith",
"status" : "ACTIVE",
"image" : {
"_id" : ObjectId("558be8062f194d2587000001")
"chunkSize" : 261120,
"uploadDate" : ISODate("2015-06-25T10:20:47.105Z"),
"length" : 25038,
"md5" : "b1d38a0b2ccde6da71fd6ae8fe2f637f",
"filename" : "mylogo.png"
}
}
How can I have a collection like this, which maintain relation between users and their images?
Note:
Database is for data
FileSystem for files
Choice Matters
Use GridFs or else use the below stuff
Instead of saving the embedded document
"image" : {
"chunkSize" : 261120,
"uploadDate" : ISODate("2015-06-25T10:20:47.105Z"),
"length" : 25038,
"md5" : "b1d38a0b2ccde6da71fd6ae8fe2f637f",
"filename" : "mylogo.png"
}
Just save the reference of the image as below
{
"_id" : "USR_1",
"username" : "willsmith123",
"firstname" : "will",
"lastname" : "smith",
"status" : "ACTIVE",
"imageRef" : "unique id (objectID)"
}
Use the imageRef to get or set the image.
When you upload image , writestream create image
id. At the same time , you can't save image id to another
collection.
If you create custom id, it's not safe and problem for
many images.
You can save image info in the document files.metadata

Remove the "ObjectId" part in a MongoDb Collection

I have a MongoDb collection named "users" with the following structure:
{
"_id" : ObjectId("akk34kt6"),
"email" : "mymail#mail.com",
"lastName" : "MyLastName",
"firstName" : "MyFirstName",
"password" : "password",
"admin" : true
}
I would like to change
"_id" : ObjectId("akk34kt6")
to
"_id" : "akk34kt6"
for every record. How can I do it from Mongo Shell ?
You can't update "_id". A possible answer to your question can be found at
How update the _id of one MongoDB Document?