Each time a user subscribe to a "street" or another "user" I want to add their Id to a nested object in The profile of the current User.
I have try the following with different results :
Meteor.users.update(Meteor.userId(), {
$addToSet: {
'profile.subscription': { Street : streetDis
}
}
})
This update the profile , but create an entry into the array each time :
Profile.subscription[0] : Street : "eziajepozjaoeja"
Profile.subscription[1] : Street : "eezapoezkaejz"
Profile.subscription[2] : User : "akzejpazjepza"
The architecture I want would be as follow :
Profile.subscription.Street[0] : "eziajepozjaoeja"
Profile.subscription.Street[1]:"eezapoezkaejz"
Profile.subscription.User[0]: "akzejpazjepza"
So I try :
Meteor.users.update(Meteor.userId(), {
$addToSet: {
'a.profile.last.Adress': "akzejpazjepza"}
}
)
Which return : update failed: Access denied
Meteor.users.update(Meteor.userId(), {
$addToSet: {
'a.profile': { last: {Adress : "akzejpazjepza"}}
}
})
This would also return : update failed: Access denied
If you want profile.subscription.streets and profile.subscription.users to be each be an array of ids, then you should update the user's document like this:
Meteor.users.update(Meteor.userId(), {
$addToSet: {
'profile.subscription.streets': streetId,
'profile.subscription.users': userId
}
});
Related
I have records like:
{
"_id" : ObjectId("5f99cede36fd08653a3d4e92"),
"accessions" : {
"sample_accessions" : {
"5f99ce9636fd08653a3d4e86" : {
"biosampleAccession" : "SAMEA7494329",
"sraAccession" : "ERS5250977",
"submissionAccession" : "ERA3032827",
"status" : "accepted"
},
"5f99ce9636fd08653a3d4e87" : {
"biosampleAccession" : "SAMEA7494330",
"sraAccession" : "ERS5250978",
"submissionAccession" : "ERA3032827",
"status" : "accepted"
}
}
}
}
How do I query by the mongo id in sample_accessions? I thought this should work but it doesn't. What should I be doing?
db.getCollection('collection').find({"accessions.sample_accessions":"5f99ce9636fd08653a3d4e86"})
The id is a key and check whether key is exists or not use $exists, customize response using project to get specific object
db.getCollection('collection').find(
{
"accessions.sample_accessions.5f99ce9636fd08653a3d4e86": {
$exists: true
}
},
{ sample_doc: "$accessions.sample_accessions.5f99ce9636fd08653a3d4e86" }
)
Playground
NOTE: Database = DB
NOTE: I'm using SWIFT 4 and Firebase 5.0.0
I am making a practice Instagram like project to practice using Firebase. I have tested the below 'DB 1' but then thought of 'DB 2' and was unsure about which one is more efficient and easier to code? The application is just supposed to upload a 'post' to a DB with 1 or 2 images. Thank you in advance!
EDIT:
Given that users would be able to see posts from people they follow and see posts from everyone on the search page. What would be an appropriate structure?
DB 1:
{
"user1" : {
"posts" : {
"post1" : {
"imgURL" : "https://firebasestorage.googleapis.com/v0/b/datamodel2-52bdf.appspot.com/o/l#gmail.com%2Fpost1%2F19B28DCA-9CD1-4079-BC75-AB2981A64CE3?alt=media&token=d984d2bd-698e-4cf4-8e42-36bf277850f2"
},
"post2" : {
"imgURL" : "https://firebasestorage.googleapis.com/v0/b/datamodel2-52bdf.appspot.com/o/l#gmail.com%2Fpost1%2F19B28DCA-9CD1-4079-BC75-AB2981A64CE3?alt=media&token=d984d2bd-698e-4cf4-8e42-36bf277850f2"
}
},
"userInfo" : {
"email" : "exmple#gmail.com",
}
},
"user2" : {
"posts" : "allposts",
"userInfo" : "email"
}
}
DB 2:
{
"UserPosts" : {
"User1ID" : {
"Post1" : {
"ImageURL1" : "URL"
}
}
},
"Users" : {
"User1ID" : {
"email" : "email"
},
"User2ID" : {
"email" : "email"
}
}
}
I have a collection (users) containing some documents like so :
{
_id: ObjectId("56d45406be05db4022be51f9"),
morecontent : ""
},
{
_id: ObjectId("56d45406be05db3021be32e3"),
morecontent : ""
}
I would like to create a new document for every entry in the user collection.
The documents would be created from a notification object like this :
{
type: 'alert',
msg: 'This is important'
}
The collection notifications should look something like this :
{
_id: ObjectId("56d45406be05db3021bf20a1"),
someoldcontent: "This was here before the request"
},
{
_id : ObjectId("56d45406be05db4022be20b1"),
user: ObjectId("56d45406be05db4022be51f9"),
type: 'alert',
msg: 'This is important'
},
{
_id : ObjectId("56d45406be05db3021be32e3"),
user: ObjectId("56d45406be05db3021be32e3"),
type: 'alert',
msg: 'This is important'
}
Is there any way to do this in a mongodb request?
Thanks to professor79 for helping me a lot with the question.
After lots of effort, the query was found. We used the aggregation framework in order to succeed.
The only 2 aggregations needed are $project and $out. The $out will automatically take care of the new _id in the notification document.
Given this collection named user :
{
_id: ObjectId("56d45406be05db4022be51f9"),
morecontent : ""
},
{
_id: ObjectId("56d45406be05db3021be32e3"),
morecontent : ""
}
We want to create a notification containing the same msg and type field for every document in the user collection.
Each notifications will be in the notifications collection an will have as a reference its corresponding userId.
Here is the query to achieve such a result :
db.user.aggregate([
{
$project: {
userId: '$_id',
type: { $literal: 'danger' },
msg: { $literal: 'This is a message' },
},
},
{ $out: 'notifications' },
])
The output in notifications collection:
{
"_id" : ObjectId("56d6112e197b4ea11a87de1a"),
"userId" : ObjectId("56d45406be05db4022be51f9"),
"type" : "danger",
"msg" : "This is a message"
},
{
"_id" : ObjectId("56d6112e197b4ea11a87de1b"),
"userId" : ObjectId("56d45406be05db3021be32e3"),
"type" : "danger",
"msg" : "This is a message"
}
As we have some chat to clarify problem:
the steps are needed to perform this operation server side:
first step - match > get users id
project ids to new documents as required
out -> store output in notification collection
I want to develop a system where users can create topics and join topics created by other users. In my exemple, they are two user ("A" and "B"), the user "A" create a topic, and the user "B" join this topic.
When "A" create a topic (logged with "A"), I initialize like this :
topic.users = [{userId: this.userId, admin: 1}];
Topics.insert(topic);
Then, when i check mongo with Topics.find(), i have something which seems good:
{ "_id" : "8FDCyKssnrGxqvAep", "users" : [ { "userId" :"vQIMLMo5CK9YqxpTZ", "admin" : 1 } ] }
When "B" (logged with "B") join the topic i call:
Topics.update(topicId ,{ $addToSet: { users: { userId: this.userId, admin: 0 }} });
the update return "undefined", and i except, after the update, to have in my document something like:
{ "_id" : "8FDCyKssnrGxqvAep", , "users" : [ { "userId" :"vQIMLMo5CK9YqxpTZ", "admin" : 1 }, { "userId" :"AlsA4q
5PS5s5ts", "admin" : 0 } ] }
I have also try to do:
Topics.update({_id:topicId, 'users.userId': {$ne: this.userId}}, {$push: {users: { userId: this.userId, admin: 0 }}});
But when i check Mongo, i still have:
{ "_id" : "8FDCyKssnrGxqvAep", , "users" : [ { "userId":"vQIMLMo5CK9YqxpTZ", "admin" : 1 } ] }
The update do nothing and they are no error message
Thanks in advance for your help
EDIT 12/12/2015:
When I try to do update in client side, it's work good.
function joinTopic(topicId)
{
var user = Meteor.user();
Topics.update(topicId ,{ $addToSet: { users: { userId : user._id, admin: 0 }} });
}
But i have a problem when i want to use call function. In client side I have:
function joinTopic(topicId)
{
Meteor.call('joinTopic', { topicId: topicId });
}
and in server side, i have:
joinTopic: function (topicId)
{
Topics.update(topicId ,{ $addToSet: { users: { userId : this.userId, admin: 0 }}});
}
OK, I found my stupid mistake !
I had to do:
Meteor.call('joinTopic', topicId);
instead of:
Meteor.call('joinTopic', { topicId: topicId });
I have a collection with multiple documents which follow this structure:
{
"_id" : {
"oid" : XXX
},
"name" : "Name",
"videos" [
{
"id" : 1,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
},
{
"id" : 2,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
},
{
"id" : 3,
"thumbnail" : "thumbnail.jpg",
"name" : "Name here"
}
]
}
I want to find and update the a thumbnail of a video, of which I only know the id, but not which document it is in.
This is what I've tried so far, but it's not working properly. All the examples I found relied on knowing the document id, and the array position of the object to update. I also found that doing a query like this found the document okay, but set the whole document as the new thumbnail!
db.collection(COLLECTION-NAME, function(err, collection){
collection.update(
{ 'videos.id' : 2 },
{ $set: { thumbnail: "newThumbnail.jpg" } },
function(err, result){
if (!err){
console.log('saved', result)
} else {
console.log('error', err);
}
}
);
});
Use the $ positional operator to update the value of the thumbnail field within the embedded document having the id of 2:
db.collection.update(
{ "videos.id": 2 },
{ "$set": { "videos.$.thumbnail" : "newThumbnail.jpg" } }
)