How to Store Images into Specified Collections in MongoDB - 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

Related

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....

Adding column in meanjs database

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.

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")}}]})

MongoDB .Net driver 2.0 Builders Filter (field to array comparison)

I need to get all usernames from "followingList.username" and compare with
posts' usernames, if there any match need to add that one to an array.
Person Model
{
"_id" : ObjectId("554f20f5c90d3c7ed42303e1"),
"username" : "fatihyildizhan",
"followingList" : [
{
"_id" : ObjectId("55505b6ca515860cbcf7901d"),
"username" : "gumusluk",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58505b6ca515860cbcf7901d"),
"username" : "yalikavak",
"avatar" : "avatar.png"
},
{
"_id" : ObjectId("58305b6ca515860cbcf7901d"),
"username" : "gumbet",
"avatar" : "avatar.png"
}
]
}
Post Model
{
"_id" : ObjectId("554f2df2a388R4b425b89833"),
"username" : "yalikavak",
"category" : "Summer",
"text" : "blue voyage with yacht"
},
{
"_id" : ObjectId("554f2df2a388P4b425b89833"),
"username" : "yalikavak",
"category" : "Winter",
"text" : "is coming ..."
},
{
"_id" : ObjectId("554f2df2a388K4b425b89833"),
"username" : "gumbet",
"category" : "Fall",
"text" : "there are many trees"
}
I try to get result code block as below but couldn't figure it out.
var filter = Builders<Post>.Filter.AnyEq("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
Can you help me with this?
Thanks.
The logic is flipped, what you need is an $in query if I understand your use case correctly:
var filter = Builders<Post>.Filter.In("username", usernameList);
var result = collection.Find(filter).ToListAsync().Result;
In your case, username is a simple field and you want to match against a list of candidates. AnyEq is used to check that, from an embedded list of complex objects, at least one matches a criterion. That still translates to a simple query in MongoDB, but requires to 'reach into' the object which needs a more complicated syntax.

want to merge two collection in mongo db using map reduce

I have two collection as bellow products has reference of user. i search product by name & in return i want combine output of product and user using map reduce method
user collection
{
"_id" : ObjectId("52ac5dd1fb670c2007000000"),
"company" : {
"about" : "This is textile machinery dealer",
"contactAddress" : [{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
},{
"address" : "abcd",
"city" : "52ac4bc6fb670c1007000000",
"zipcode" : "39as46as80"
}],
"fax" : "58784868",
"mainProducts" : "ads,asd,asd",
"mobileNumber" : "9537236588",
"name" : "krishna steels",
}
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
product colletion
{
"_id" : ObjectId("52ac5722fb670cf806000002"),
"category" : "52a2a9cc48a508b80e00001d",
"deliveryTime" : "10 days after received the ",
"price" : {
"minPrice" : "2000",
"maxPrice" : "3000",
"perUnit" : "5288ac6f7c104203e0976851",
"currency" : "INR"
},
"productName" : "New Mobile Solar Charger with Carabiner",
"rejectReason" : "",
"status" : 1,
"user" : ObjectId("52ac4eb7fb670c0c07000000")
}
This cannot be done. Mongo support Map Reduce only on one collection. You could try to fetch and merge in a java collection. Couple of days back I solved a similar problem using java collection.
Click to see similar response about joins and multi collection not supported in mongo.
This can be done using two map reduces.
You run your first MR and then you reduce out the second MR onto the results of the first.
You shouldn't do this though. JOINs are not designed to be done through MR, in fact it sounds like you are trying to do this MR with inline output which in itself is a very bad idea.
MRs are not designed to run inline to the application.
You would be better off doing the JOIN else where.