Unique array values in Mongoose - arrays

Currently trailing out Mongoose and MongoDB for a project of mine but come across a segment where the API is not clear.
I have a Model which contains several keys and documents, and one of those keys os called watchList. This is an array of ID's that the user is watching, But I need to be sure that these values stay unique.
Here is some sample code:
var MyObject = new Mongoose.Schema({
//....
watching : {type: Array, required: false},
//....
});
So my question is how can I make sure that the values pushed into the array only ever store one, so making the values unique, can i just use unique: true ?
Thanks

To my knowledge, the only way to do this in mongoose is to call the underlying Mongo operator (mentioned by danmactough). In mongoose, that'd look like:
var idToUpdate, theIdToAdd; /* set elsewhere */
Model.update({ _id: idToUpdate },
{ $addToSet: { theModelsArray: theIdToAdd } },
function(err) { /*...*/ }
);
Note: this functionality requires mongoose version >= 2.2.2

Take a look at the Mongo documentation on the $addToSet operator.

Mongoose is an object model for mongodb, so one option is to treat the document as a normal javascript object.
MyModel.exec(function (err, model) {
if(model.watching.indexOf(watchId) !== -1) model.watching.push(watchId);
model.save(...callback);
});
Although, I do agree that mongoose should have some support for this built in the form of a validator for the collection document reference feature-- especially because most of the time you want to add only unique references.

That's how you can do it using Mongoose,
IF your upcoming value is an Array
Model
.findOneAndUpdate({ _id: yourID },
{ $addToSet: { watching: { $each: yourWatchingArr } } },
function(err) { /*...*/ }
);
IF your upcoming value is a string
Model
.findOneAndUpdate({ _id: yourID },
{ $addToSet: { watching: yourStringValue } },
function(err) { /*...*/ }
);

Related

Use nestjs to add unique ObjectID to each document that doesn't have one

Just looking for a bit of guidance. I am connected to my local mongoDB instance and I am trying to build a query that will add a unique objectID to every object in an array that doesn't have an _id already. I am trying to do this in nestjs so I can utilize javascript and get a unique ObjectID. Here is what I have so far.
const temp = await this.vehiclesRepository.updateMany(
{
$stipulations: [],
},
{
$set: {
$stipulations: {
$function: {
body: "function (ar) {return ar.map(x => { if(x.hasOwnProperty('_id')) return x; else {x['_id']=new ObjectId(); return x;}})}",
args: ['$stipulations'],
lang: 'js',
},
},
},
},
);
public async updateMany(filter: FilterQuery<T>, doc: UpdateQuery<T>) {
this.model.updateMany(filter, doc, { upsert: true, multi: true });
}
Can anyone tell me where I am going wrong with this? Based on my research this seems like the proper method to accomplish this task.
The $function operator is an aggregation operator. You should use it within an aggregation pipeline. There is also a $set operator specifically meant for aggregations.
You should combine the $function and $set (aggregation) operators within an aggregation pipeline.
I was able to answer my question in the MongoDB shell, with no javascript like so
db.vehicles.find().forEach(function(doc){
if(doc.stipulations != null) {
if(doc.stipulations.length > 0) {
doc.stipulations.forEach(function(stip) {
if(stip._id == null) {
stip._id = new ObjectId();
}
});
}
}
db.vehicles.updateOne({_id: doc._id}, {$set: doc});
});
For optimization it is possible to filter purely in find(), but for this one time query I am not posting that.

How to update an object inside an array inside another array in a mongoose schema?

found a lot of questions like these here, but not an answer.
Problem
Lets say I have the following mongoose schema:
const mySchema = new mongoose.Schema({
sanePeoplesField: String,
comments: [
normalStuff: {type: Date, default: Date.now},
damNestedAgain: [String]
]
})
So to recap, damNested array is inside the comments array on the schema.
If I was lucky and wanted to change normalStuff (obj inside an array), I'd do this:
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
comments: { normalStuff: 12122020 } }
}
})
This would've updated normalStuff with a new value.
However, I need to update a field in damNestedAgain, but don't know how to reach it!
Question
How to update the nested array of a nested array, damNestedAgain, in my example?
mySchema.findOneAndUpdate({"comments._id": req.body.commentId},
{
$push:
{
"comments.$.damNestedAgain": req.body.commentId
}
})
That has done the trick, thanks.

Push/Pull values on nested array mongoose

Im trying to update a value in a nested Array in a mongoose schema using express. I have the code required in place what i figuered i needed to update it, but the array doesn't get updated.
So the idea is to be able to have an array of data base schema objects with two fields, schemaName and schemaFields. I want to be able to update (add/remove)the values from schemaFields field of a specific schema object as needed.
I've already tried a bunch of stuff on here and elsewhere on the internet but nothing appears to work. I tried using findOneAndUpdate, findByIdAndUpdate etc.
My mongoose schema is as follows,
let databaseSchema = new Schema({
schemaName: { type: String },
schemaFields: [String]
});
let databaseSchemas = new Schema(
{
dbSchemas: [databaseSchema]
},
{
collection: 'databaseSchemas'
}
);
my update function is as follows,
schemasModel.mongo
.update(
{
_id: req.body.documentId,
'dbSchemas._id': req.body.schemaId
},
console.log('preparin to push the field \n'),
{
$push: {
'dbSchemas.$.schemaFields': req.body.newField
}
}
)
.then(() => {
res.send('new field added successfully');
});
So I solved it by removing the console.log() as a second argument to the model.update() function. Apparently this has to be the object with the operation.
The working code for the Model.update function is as follows,
schemasModel.mongo
.update(
{
_id: req.body.documentId,
'dbSchemas.schemaName': req.body.schemaToSearch
},
{
$push: {
'dbSchemas.$.schemaFields': req.body.newField
}
}
)
.then(() => {
res.send('new field added successfully');
});

Pushing onto Mongo SubDoc of SubDoc array

I'm going around in circles with this one so hoping someone can help. I'm building a nodejs application that receives sensor values from nodes. There can be multiple sensors on a node.
Using NodeJS, Mongod DB and Mongoose, all running on a raspberry pi, 3 I've built the following Schemas & Model:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var valueSchema = new Schema ({
timestamp: {type: Date},
value: {}
});
var sensorSchema = new Schema ({
id: {type: Number},
type: {type: String},
description: {type: String},
ack: {type: Boolean},
values: [valueSchema]
});
var SensorNode = mongoose.model('SensorNode', {
id: {type: Number, required: true},
protocol: {},
sensors: [sensorSchema]
});
I can add in the node, and push sensors onto the sensors array, but I seem unable to push values onto the values array.
I've looked over a few other examples and questions on similar issues, and looked at using populate, but cant seem to get them to work.
Here is my code:
function saveValue(rsender, rsensor, payload) {
var value = {
values: {
timestamp: new Date().getTime(),
value: payload
}
}
SensorNode.findOneAndUpdate({
"id": rsender,
"sensors.id": rsensor
}, {
"$push": {
"sensors.$": value
}
}, function(err, result) {
if (err) {
console.log(err);
}
console.log(result);
});
}
This is returning undefined for the result and this error:
MongoError: exception: Cannot apply $push/$pushAll modifier to non-array
Values is definitely an array in the sensor schema.
I'm using readable ids rather than the auto assigned Mongo DB IDs for the sake of the UI, but I could use the MongoDB _id if that makes any difference, I don't see why it would?
Where am I going wrong ?
You're using positional operator $ so let's check the docs
The positional $ operator identifies an element in an array to update without explicitly specifying the position of the element in the array. To project, or return, an array element from a read operation, see the $ projection operator.
So sensors.$ will return one particular document from your sensors array. That's why you're getting an error. On this level of your document you can only replace this item by using $set. I bet you wanted to do something like this:
SensorNode.findOneAndUpdate({
"id": rsender,
"sensors.id": rsensor
}, {
"$push": {
"sensors.$.values": payload
}
});
This operation will just append payload to values array in one particular sensor with id equal to rsensor.

Meteor, MongoDB get part of array through subscription

I have a question about how to just get a certain element of an array using MongoDB and MeteorJS. I have the following schema for the user document:
bankList:[
{
id: "34567890987654345678",
name: "xfgchjbkn",
type: "credit"
},
{
id: "09876543456789098767"
name: "65789876t8",
type: "debit"
}
]
I first subscribe to only part of the fields in the array, specifically I gather a list of all the ids. Then I have an edit screen that should subscribe to all of the fields for a specific element in the array with a matching id. I do not want to expose the rest of the array just the single element. Currently, I use the following to first gather a list of just the ids:
Meteor.users.find({_id: this.userId},
{fields:{'bankList.id': 1}});
And the following publication-subscription method to get just a specific element's information:
Publication:
Meteor.publish("userBankAdvanced", function(bankId){
check(bankId,String);
if(this.userId){
return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {'bankList.$': 1});
}else{
this.ready();
}
});
Subscription:
this.route('edit_account', {
path: '/edit/account/',
waitOn: function(){
if(Session.get("bankId")){
return Meteor.subscribe('userBankAdvanced',Session.get("bankId"));
}
return null;
},
data: function(){
if(Session.get("bankId")){
return Meteor.users.findOne();
}
return null;
},
onBeforeAction: function(){
beforeHooks.isRevise(Session.get("bankId"));
}
});
The subscription method returns all of the elements of the array with all of the information.
I want, for example, just this (not the entire list with all of the information):
bankList:[
{
id: "34567890987654345678",
name: "xfgchjbkn",
type: "credit"
}]
It looks like you're just missing the "fields" specifier in your "userBankAdvanced" publish function. I wrote a test in meteorpad using your example and it seems to work fine. The bank id is hardcoded for simplicity there.
So instead of
return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {'bankList.$': 1});
try using
return Meteor.users.find({_id:this.userId,"bankList.id": bankId}, {fields: {'bankList.$': 1}});
No luck, in meteor the "fields" option works only one level deep. In other words there's no builtin way to include/exclude subdocument fields.
But not all is lost. You can always do it manually
Meteor.publish("userBankAdvanced", function (bankId) {
var self = this;
var handle = Meteor.users.find({
_id: self.userId, "bankList.id": bankId
}).observeChanges({
added: function (id, fields) {
self.added("users", id, filter(fields, bankId));
},
changed: function (id, fields) {
self.changed("users", id, filter(fields, bankId));
},
removed: function (id) {
self.removed("users", id);
},
});
self.ready();
self.onStop(function () {
handle.stop();
});
});
function filter(fields, bankId) {
if (_.has(fields, 'bankList') {
fields.bankList = _.filter(fields.bankList, function (bank) {
return bank.id === bankId;
});
}
return fields;
}
EDIT I updated the above code to match the question requirements. It turns out though that the Carlos answer is correct as well and it's of course much more simple, so I recommend using that one.

Resources