Meteor publication: Hiding certain fields in an array document field? - mongodb

I have a collection with documents like this:
{
_id: af3F3afafaa,
firstName: "John",
family: [{name: "David", relation: "brother", alive: true},
{name: "Susan", relation: "mother", alive: false}]
}
Is there a way to write a publication that hides a field in the family field array? So if I subscribed to the publication I would get:
{
_id: af3F3afafaa,
firstName: "John",
family: [{name: "David", alive: true},
{name: "Susan", alive: false"}]
}

According to the Meteor docs, something like this could work:
Meteor.publish('family', function(famId) {
return Families.find(famId, {
fields : {
"family.relation" : 0 //Exclude family.relation from the sent data
}
});
});

Related

How to update a property of object in a nested array in MongoDB?

I am trying to update a property from nested array object. I use this code but not working.
This is my data
[
{
_id: ObjectId("630a869a887d39ef359ef6e6"),
firstName: 'Esra',
lastname: 'Demirci',
age: 33
},
{
_id: ObjectId("630a86a5887d39ef359ef6e7"),
firstName: 'Efe',
lastname: 'Demirci',
age: 7,
history: [ { disease: 'flue', threatment: 'given serum' } ]
},
{
_id: ObjectId("630a8e10887d39ef359ef6e8"),
firstName: 'Ali',
lastname: 'Demirci',
age: 34,
history: [
{ disease: 'cold', threatment: 'medicine given' },
{ disease: 'alergy', threatment: 'refered alergy department' }
]
}
]
This is my query in mongodb
db.patientData.update({"history.disease":"cold"},{$set:{"history.disease":"ali updated this one"}})
I wanna update data which has disease:"cold" value to new value.
Try using the $ sign when updating in MongoDB, like so:
db.patientData.update({"history.disease":"cold"},{$set:{"history.$.disease":"ali updated this one"}})

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

Should I use separate collection or embed fields that I know won't be used for all models. MongoDB

Background:
I'm planning an app that will have 3 types of posts, for n number of games; Single-posts, team-posts, coach-posts. Now I'm not sure of the best Schema for a single type of post.
The posts of a certain type share a couple fundamental attributes, like: user_id, comments, status, etc. But the fields relevant to the game will be unique.
These are the two possibilities I'm considering:
1. Separate collection for each game:
As you can see the playerposts type requires different fields for each game but has a similar structure.
// game1_playerposts
{
_id: ObjectId(),
user_id: ObjectId(),
game: ObjectId(),
comments: [{
user_id: ObjectId(),
comment: String,
score: Number
}],
rank: {
name: String,
abbr: String,
img: String
},
roles: [String],
gamemode: [String]
}
// game2_playerposts
{
_id: ObjectId(),
user_id: ObjectId(),
game: ObjectId(),
comments: [{
user_id: ObjectId(),
comment: String,
score: Number
}],
level: {
name: String,
abbr: String,
img: String
},
champions: [String],
factions: [{
name: String,
abbr: String,
img: String
}]
}
2. One collection for all games:
This way I only need one collection, and will always only use the fields I need, and the rest would remain empty.
{
_id : ObjectId(),
user_id : ObjectId(),
game1 : {
game: ObjectId(),
rank: {
name: String,
  abbr: String,
img: String
},
roles: [String],
gamemodes: [String]
},
game2 : {
game: ObjectId(),
level: {
name: String,
  abbr: String,
img: String
},
champions: [String],
factions: {
name: String,
  abbr: String,
img: String
}
},
game_n {
...
},
comments : [{
user_id: ObjectId(),
comment: String,
score: Number
}],
}
What's better?
Which one of these options would be better suited? Performance is important, but I also want it to be simple to add to the Schema when we decide to add support for another game in the future.
MongoDB is schemaless.
I don't see why you have to have fields you know won't be used. Why not just have a separate document for each individual player post and that document will have the schema that relates to the type of post it is?
You can have in a single collection both of the documents that you have as examples under the "Separate collection for each game" header.
I have not worked with Mongoose, but if using it removes the benefits of MongoDB being schemaless, I don't think it would be as popular a tool as it is, so I think there's a way for it to work.

mongodb find query conditional return value

I can do a find in mongodb like:
db.user.find({ "show": true}, { firstname: 1, lastname: 1, email:1 })
Now I'd like to be a little more specific to let people hide their email address.
So what I'd like is to query if "show":true then get firstname:1, lastname:1 AND (email:1 iff show_email:true)
worst case I can take all the info and filter it out on the back end with a map function but I wondered if there was a nice query-able way??
The truthiness of the projection object's fields are all that matter, so you can just do it as:
db.user.find({ "show": true }, { firstname: 1, lastname: 1, email: show_email })
Since you didnt specify any driver specification, I am betting on this, the MongoShell
if(show_email){
db.user.find({ "show": true}, { firstname: 1, lastname: 1, email:1 })
}
else{
db.user.find({ "show": true}, { firstname: 1, lastname: 1})
}

MongoDB: Appending a nested array structure inside a nested array in mongoDB

I have a collection in mongoDB containing many documents, One of which looks like,
{
date: '2012-10-20 12:30UTC',
personal_info: {name: 'sam', group_id: 'A1234', address:['rourkela', 'sundergarh']},
blog:[{ blog: 'Got admitted to IIT',
comments:[{
author:{name:'sam', email: 'sam#gmail.com'},
comment:'good buddy',
ph_no: 12345
},
{
author:{name:'vam', email: 'vam#gmail.com'},
comment:'happy for you',
ph_no:56789}
}]
}]
}
My requirement is to append a sub document like structure into the comments array where blog is 'Got admitted to IIT'
The Sub document I wand to append is given below:
{
author:{name:'mam', email: 'mam#gmail.com'},
comment:'great, keep it up'
}
My document structure after appending the structure above should look like:
{
date: '2012-10-20 12:30UTC',
personal_info: {name: 'sam', group_id: 'A1234', address:['rourkela', 'sundergarh']},
blog:[{ blog: 'Got admitted to IIT',
comments:[{
author:{name:'sam', email: 'sam#gmail.com'},
comment:'good buddy',
ph_no: 12345
},
{
author:{name:'vam', email: 'vam#gmail.com'},
comment:'happy for you',
ph_no:56789
},
{
author:{name:'mam', email: 'mam#gmail.com'},
comment:'great, keep it up'
}]
}]
}
How do I achieve the above by Coding in mongo shell.
Thanks in advance..
You can do it as follows :
db.myObject.update(
{"blog.blog" : "Got admitted to IIT"},
{
"$push" : {
"blog.$.comments" : {
author: {name:'mam', email: 'mam#gmail.com'},
comment:'great, keep it up'
}
}
}
)