updating mongodb using $push operator in angular-meteor - javascript

I am trying to push arrays from one collection to another.
this is the code I used in my server js
updateSettings: function(generalValue) {
let userId = Meteor.userId();
let settingsDetails = GeneralSettings.findOne({
"_id": generalValue
});
Meteor.users.update({
_id: userId
}, {
$push: {
"settings._id": generalValue,
"settings.name": settingsDetails.name,
"settings.description": settingsDetails.description,
"settings.tag": settingsDetails.tag,
"settings.type": settingsDetails.type,
"settings.status": settingsDetails.status
}
})
}
updateSettings is the meteor method. GeneralSettings is the first collection and user is the second collection. I want to push arrays from GeneralSettings to users collection. While I try this the result i got is like
"settings" : {
"_id" : [
"GdfaHPoT5FXW78aw5",
"GdfaHPoT5FXW78awi"
],
"name" : [
"URL",
"TEXT"
],
"description" : [
"https://docs.mongodb.org/manual/reference/method/db.collection.update/",
"this is a random text"
],
"tag" : [
"This is a demo of an Url selected",
"demo for random text"
],
"type" : [
"url",
"text"
],
"status" : [
false,
false
]
}
But the output I want is
"settings" : {
"_id" : "GdfaHPoT5FXW78aw5",
"name" : "URL",
"description" :
"https://docs.mongodb.org/manual/reference/method/db.collection.update/",
"tag" :"This is a demo of an Url selected",
"type" : "url",
"status" : false
},
What changes to be made in my server.js inorder to get this output

This is one case where you "do not want" to use "dot notation". The $push operator expects and Object or is basically going to add the "right side" to the array named in the "left side key":
// Make your life easy and just update this object
settingDetails._id = generalValue;
// Then you are just "pushing" the whole thing into the "settings" array
Meteor.users.update(
{ _id: userId },
{ "$push": { "settings": settingDetails } }
)
When you used "dot notation" on each item, then that is asking to create "arrays" for "each" of the individual "keys" provided. So it's just "one" array key, and the object to add as the argument.

Related

Replace field in mongo db database

Helloo guys,
i have in mongo db collection:
{
"_id" : "aghpbF6xxxCyMGg9J",
"description" : "",
"name" : "test",
"roles" : [
"ab.read",
"test.view"
],
"type" : "group"
}
and i have testRoles object:
[ { name: 'testRoles',
permissions: [ 'ab.write', 'test.check' ] } ],
and basically what i need to do is, replace field roles in db with field permissions from testRoles object, please anybody can help me manage it please?
You can simply $set the desired field and replace the entire array like this:
db.collection.updateOne(
{} // empty match since idk what your match parameter is ;)
,{
$set: {
"roles": ['a', 'b'] // this is your value from the object
}
})

Apply an expression to each array element in document

I've got a sample document that I'm trying to project within a MongoDB aggregate pipeline. I'm testing with a single document that looks roughly like this:
{
"_id" : "",
"title" : "Questions",
"sortIndex" : 0,
"topics" : [
{
"_id" : "",
"title" : "Creating a Question",
"sortIndex" : 1,
"thumbnail" : "CreatingAQuestion.jpg",
"seenBy" : [ "user101", "user202" ],
"pages" : [
{
"visual" : "SelectPlanets.gif",
"text" : "Some Markdown"
}
]
},
{
"_id" : "",
"title" : "Deleting a Question",
"sortIndex" : 0,
"thumbnail" : "DeletingAQuestion.jpg",
"seenBy" : [ "user101" ],
"pages" : [
{
"visual" : "SelectCard.gif",
"text" : "Some Markdown"
}
]
}
]
}
The output I'm trying to obtain is something along these lines:
{
"_id" : "",
"title" : "Questions",
"topics" : [
{
"title" : "Creating a Question",
"thumbnail" : "CreatingAQuestion.jpg",
"seen" : true
},
{
"title" : "Deleting a Question",
"thumbnail" : "DeletingAQuestion.jpg",
"seen" : false
}
]
}
Specifically the bit I'm struggling with is the seen flag.
I've read the docs which state:
When projecting or adding/resetting a field within an embedded document...
... Or you can nest the fields:
contact: { address: { country: <1 or 0 or expression> } }
I wish to use an expression and I took note of the following:
When nesting the fields, you cannot use dot notation inside the embedded document to specify the field, e.g. contact: { "address.country": <1 or 0 or expression> } is invalid.
So I'm trying to work out how to "reference" a subdocument field within an expression. That quote suggests I can't use dot notation but when I can't seem to get it working with nested notation either. Here's what I've got so far:
db
.getCollection('chapters')
.aggregate([
{
$project: {
title: 1,
topics: {
title: 1,
thumbnail: 1,
publishedAt: 1,
test: "$seenBy",
seen: { $in: ["user202", "$seenBy"] },
}
}
}
])
So I've hard coded user202 into my query for now, and expected to see true and false for the 2 documents. I've also put in a test field to map out the seenBy field from the sub-document. What this produces is:
{
"_id" : "",
"title" : "Questions",
"topics" : [
{
"title" : "Creating a Question",
"thumbnail" : "CreatingAQuestion.jpg",
"test" : [
"user101",
"user202"
],
"seen" : true
},
{
"title" : "Deleting a Question",
"thumbnail" : "DeletingAQuestion.jpg",
"test" : [
"user101",
"user202"
],
"seen" : true
}
]
}
So obviously my "$seenBy" isn't accessing the correct topic because the test field contains the data from the 1st document.
So ultimately my question is, how can I access the seenBy field within a subdocument, referring to the current subdocument so I can create an expression?
Note: I have got this working with multiple $project and an $unwind but wanted to try compress/clean it up a bit.
You really need to use $map here. Simply notating the array in projection ( which is a bit of a boon since MongoDB 3.2 ) does not really cut it when you need a localized value for the current element. That is what you need and it's what $map provides:
db.getCollection('chapters').aggregate([
{ $project: {
title: 1,
topics: {
$map: {
input: "$topics",
as: "t",
in: {
title: "$$t.title",
thumbnail: "$$t.thumbnail",
publishedAt: "$$t.publishedAt",
test: "$$t.seenBy",
seen: { $in: ["user202", "$$t.seenBy"] },
}
}
}}
])
So for each element the current value of "seenBy" as a property is being tested by the expression. Without the $map that is not possible, and you can only really notate the "whole" array. Which is really not what you want to test here.

What is causing "The dollar ($) prefixed field '$conditionalHandlers' in 'collaborators..$conditionalHandlers' is not valid for storage."

I am writing a Node/Express/Mongoose (latest versions) application which has "Projects" with a list of "collaborators" which are IDS of "Users". Until now, I've been storing the list of foreign keys as hex strings. This is now making it difficult to perform some slightly more complex aggregation, so I have decided to store them as ObjectId type instead, which makes the joins simpler.
In the function which creates the array, the push(userId) version works fine, adding collaborators to the array. However pushing an ObjectId into the array, or assigning an array containing an ObjectId fails with
"The dollar ($) prefixed field '$conditionalHandlers' in
'collaborators..$conditionalHandlers' is not valid for storage."
function addCollaborator(projectId, userId, fn){
projectModel.findById(projectId, (err, project)=>{
if(err) return fn(err);
project.collaborators.push( new Schema.Types.ObjectId(userId)); // errors
// project.collaborators = [ new Schema.Types.ObjectId(userId) ]; // errors
// project.collaborators.push( userId); // works
project.save((err)=>{
logService.error('Error adding collaborator to project: '+err.toString());
});
fn(null);
});
}
Project model:
const ProjectSchema = new mongoose.Schema({
name: String,
create_date: Date,
administrators: Array, // list of user._id
collaborators: Array, // list of user._id ObjectIds
});
With the text IDs, I get projects looking like:
{ "_id" : ObjectId("594e2222a26ca3505c18c674"),
"name" : "Pips 2nd Project", "create_date" : ISODate("2017-06-24T08:26:10.498Z"),
"collaborators" : [ "5936a3576d6c5a3ef4ee0936" ],
"administrators" : [ "594dbba8186f1a2f5ad7539c" ], "__v" : 1 }
When it breaks, I log the error, and am left with an empty array:
{ "_id" : ObjectId("594e278b6a68a2815b043bd1"),
"name" : "Pips third Project", "create_date" : ISODate("2017-06-24T08:49:15.091Z"),
"collaborators" : [ ],
"administrators" : [ "594dbba8186f1a2f5ad7539c" ], "__v" : 0 }
What I want to achieve is:
{ "_id" : ObjectId("594e2222a26ca3505c18c674"),
"name" : "Pips 2nd Project", "create_date" : ISODate("2017-06-24T08:26:10.498Z"),
"collaborators" : [ Object("5936a3576d6c5a3ef4ee0936") ],
"administrators" : [ "594dbba8186f1a2f5ad7539c" ], "__v" : 1 }
I've seen a few other SO's or github issues, but none seem to explain the problem. This one has the same problem, but "solved" it by using strings - which is the opposite of my issue.
After reading some other posts (e.g.), I realised I was using the wrong method to create an ObjectId from a hex string.
new Schema.Types.ObjectId(userId) // not right!
Should be:
mongoose.Types.ObjectId(userId)
So this works as expected now:
project.collaborators.push( mongoose.Types.ObjectId(userId));
and produces:
{ "_id" : ObjectId("594e278b6a68a2815b043bd1"),
"name" : "Pips third Project", "create_date" : ISODate("2017-06-24T08:49:15.091Z"),
"collaborators" : [ ObjectId("5936a3576d6c5a3ef4ee0936") ],
"administrators" : [ "594dbba8186f1a2f5ad7539c" ], "__v" : 1 }

How to get element from collection in Meteor if it multi-dimensional array or object or both?

I have collection "groups". like this:
{
"_id" : "e9sc7ogDp8pwY2uSX",
"groupName" : "one",
"creator" : "KPi9JwvEohKJsFyL4",
"eventDate" : "",
"isEvent" : true,
"eventStatus" : "Event announced",
"user" : [
{
"id" : "xfaAjgcSpSeGdmBuv",
"username" : "1#gmail.com",
"email" : "1#gmail.com",
"order" : [ ],
"price" : [ ],
"confirm" : false,
"complete" : false,
"emailText" : ""
},
...
],
...
"buyingStatus" : false,
"emailTextConfirmOrder" : " With love, your Pizzaday!! "
}
How can I get a value of specific element? For example i need to get value of "Groups.user.confirm" of specific group and specific user.
I tried to do so in methods.js
'pizzaDay.user.confirm': function(thisGroupeId, thisUser){
return Groups.find({ _id: thisGroupeId },{"user": ""},{"id": thisUser}).confirm
},
but it returns nothing.
Even in mongo console I can get just users array using
db.groups.findOne({ _id: "e9sc7ogDp8pwY2uSX"},{"user": ""})
The whole code is github
http://github.com/sysstas/pizzaday2
Try the following query:-
db.groups.aggregate(
[
{
$match:
{
_id: thisGroupeId,
"user.id": thisUser
}
},
{
$project:
{
groupName : 1,
//Add other fields of `user` level, if want to project those as well.
user:
{
"$setDifference":
[{
"$map":
{
"input": "$user",
"as": "o",
"in":
{
$eq : ["$$o.id" , thisUser] //Updated here
}
}
},[false]
]
}
}
}
]);
The above query will give the object(s) matching the query in $match inside user array. Now you can access any field you want of that particular object.
'pizzaDay.user.confirm': function(){
return Groups.findOne({ _id: thisGroupeId }).user.confirm;
I resolved it using this:
Template.Pizzaday.helpers({
confirm: function(){
let isConfirm = Groups.findOne(
{_id: Session.get("idgroupe")}).user.filter(
function(v){
return v.id === Meteor.userId();
})[0].confirm;
return isConfirm;
},
});
But I still think that there is some much elegant way to do that.

Search MongoDB to return all values for a parameter

I'm running an app built with Angular, built on a Node/Express/MongoDB stack.
In my Mongo database, I have objects that look like this:
{
"infoLink" : "http://foo.com",
"name" : "Fossil Watch",
"gender" : "mens",
"price" : "$166.44",
"store" : "Store Name",
"designer" : "Fossil",
},
{
"infoLink" : "http://foo.com",
"name" : "Timex Watch",
"gender" : "mens",
"price" : "$166.44",
"store" : "Store Name",
"designer" : "Timex",
},
{
"name" : "Casio Watch",
"gender" : "mens",
"price" : "$166.44",
"store" : "Store Name",
"designer" : "Casio",
}
I want to find a way to search mongoDB to find and list all of the elements from one parameter. For instance, I want to search through all entries and extract their "designer" value, so as to return:
record = ['Fossil', 'Timex', 'Casio']
In practice I have about 20,000 records that I'm searching through here, so I'd like it to be as speedy and efficient as possible.
Is there a way to do this without resorting to getting all records then looping through each to find the desired parameter and adding it to an array? That solution would be like this:
var arr=[];
db.products.find(criteria, function (err, record) {
if (err) {
console.log("Lookup Error: " + err);
} else{
record.forEach(function(data){
if (arr.indexOf(data.designer) > -1){
arr.push(data.designer);
}
});
}
});
but I feel like this is a pretty clunky way to do it. Any better options?
How about using a distinct query:
db.products.distinct("designer")
db.collection.distinct()

Categories