Update existing items in nested array using mongoose - node.js

There is a schema as mentioned below. I'm trying to update the existing todo.task.
The problem is, I am storing the path as var done = 'todos.'+todoIndex+'.tasks.'+taskIndex+'.done' and it does not work. I was looking to update like todos.0.tasks.0.done:req.body.done, but it doen't work at all.
(todoIndex and taskIndex are in string which stores the index values)
What is the correct way of doing this?
var mongoose = require('mongoose');
var todoSchema = {
authorId : mongoose.Schema.Types.ObjectId,
date:{
type:Date
},
title : {
type : String
},
description : {
type : String
},
todos : [ {
created : {
type : Date
},
updated : {
type : Date
},
title : {
type : String
},
description : {
type : String
},
done:{
type:Boolean
},
deadline:{
type:Date
},
tasks : [ {
done : Boolean,
task : String
} ]
} ]
}
module.exports = new mongoose.Schema(todoSchema);
module.exports.todoSchema = todoSchema;
I was trying to build the Api like this:
api.put('/notebooks/todo/update/:pid',wagner.invoke(function(Todo,User){
return function(req,res){
var taskIndex=req.body.taskIndex;
var todoIndex=req.body.todoIndex;
var done = 'todos.'+todoIndex+'.tasks.'+taskIndex+'.done';
console.log(done);
Todo.update({_id:req.params.pid},{$set:{
done : req.body.done,
}}, function(err,done){
console.log( done);
})
}}));

If you're using a recent Node version, you can use a computed property name:
Todo.update({ _id : req.params.pid }, { $set : { [ done ] : req.body.done } }, ...
Otherwise, you need to use an intermediate object:
var done = 'todos.'+todoIndex+'.tasks.'+taskIndex+'.done';
var obj = {};
obj[done] = req.body.done;
Todo.update({ _id : req.params.pid }, { $set : obj }, ...

Related

writing filter in loopback

I'm trying to write a loopback 4 filter that returns the objects where creatorId = userId OR userId in sharedUsers = userId but I cant seem to formulate the filter correctly
(neither of these properties are unique id's)
this is what my object shape looks like:
{
"_id" : "20",
"configMetadata" : {
...
"creatorId" : "50",
"creatorName" : "Mark"
},
"sharedUsers" : [
{
"userId" : "15"
},
{
"userId" : "20"
}
],
"sharedRoles" : ....,
"tiles" : ...
}
here is what I tried
const filter2: Filter<ProductViewConfig> =
{
where: {
or: [
{ configMetadata : { creatorId : userId} },
{ sharedUsers: { [userId]: userId } },
],
},
};
but I'm getting 2 errors: one is : configMetadata is missing the rest of the properties, so just properties mismatch and for sharedUsers I'm getting a MongoError: unknown operator: $15 when I enter userId = 15
to provide context: this is what my endpoint looks like
#get('/product-view-configs/{userId}')
#response(200, {
description: 'ProductViewConfig model instance',
content: {
'application/json': {
schema: getModelSchemaRef(ProductViewConfig),
},
},
})
async findIt(
#param.path.string('userId') userId: string,
#param.filter(ProductViewConfig, { exclude: 'where' }) filter?: FilterExcludingWhere<ProductViewConfig>
): Promise<ProductViewConfig> {
//returns configs where creatorId = userId OR userId in sharedUsers = userId
const filter2: Filter<ProductViewConfig> =
{
where: {
or: [
{ configMetadata : { creatorId : userId} },
{ sharedUsers: { [userId]: userId } },
],
},
};
const records = this.productViewConfigRepository.find(filter2);
return this.productViewConfigRepository.findById(userId, filter);
}
can anyone point me in the right direction? I couldn't find examples on how to filter nested objects in the documentation, so any help would be appreciated!

How do i retrieve just the child-object in Azure Cosmos using mongoose and Node.js?

I am using Azure cosmos db with the Mongodb API. Also i am using mongoose to create schemas and create new documents in the database. I am also using Node.js.
At this point I am considering using a One-to-Many relationship with embedded documents.
The data structure is like this :
{
"_id" : "locality1",
"_type" : "Locality",
"name" : "Wallmart",
"subsectionList" : [
{
"_id" : "subsection1",
"_type" : "SubSection",
"name" : "First floor",
"sensorList" : [
{
"_id" : "sensor1",
"_type" : "Sensor",
"placement" : "In the hallway"
},
{
"_id" : "sensor2",
"_type" : "Sensor",
"placement" : "In the ceiling"
}
]
},
{
"_id" : "subsection2",
"_type" : "SubSection",
"name" : "Second floor",
"sensorList" : [ ],
}
],
}
I want to retrieve ONLY the "sensor1"-object, not anything from the parent.
Using querying i am only able to retrieve the entire "locality1"-object, with all its underlying subsections and sensors. On a larger scale that is an unnecessary large amount of data.
Here is my query so far.
Locality.find().where('subsectionList.sensorList._id').equals("sensor1").then(doc => {
console.log(doc)
})
I appreciate any tips! :)
Based on my test, i can't get rid of the _id property anyway even though i followed the parameters which is mentioned here.
Locality.find({},'subsectionList', function (err, locas)
The above query still return the results including _id property.(It seems a default item)
I get a workaround from this blog that you could loop the array to filter your desired columns.
var mongoose = require('mongoose');
var COSMOSDB_CONNSTR= "mongodb://***.documents.azure.com:10255/db";
var COSMODDB_USER= "***";
var COSMOSDB_PASSWORD= "***";
mongoose.connect(COSMOSDB_CONNSTR+"?ssl=true&replicaSet=globaldb", {
auth: {
user: COSMODDB_USER,
password: COSMOSDB_PASSWORD
}
}).then(() => console.log('Connection to CosmosDB successful'))
.catch((err) => console.error(err));
const Locality = mongoose.model('Locality', new mongoose.Schema({
_id: String,
subsectionList: [{
sensorList: [{
_id: String,
_type: String,
placement: String
}]
}]
}));
Locality.find({},'subsectionList', function (err, locas) {
if (err) return handleError(err);
var returnArray = [];
for(var i = 0; i<locas.length;i++){
for(var j = 0; j<locas[i].subsectionList.length;j++){
for(var k = 0; k<locas[i].subsectionList[j].sensorList.length;k++){
if(locas[i].subsectionList[j].sensorList[k]._id == 'sensor1')
returnArray.push(locas[i].subsectionList[j].sensorList[k]);
}
}
}
console.log(returnArray);
});

Skip one nested level of subdocument in Mongoose

I have a parent schema with a subdocument. The subdocument has a property with an array of embedded objects:
Child schema
var snippetSchema = new Schema({
snippet: [
{
language: String,
text: String,
_id: false
}
]
});
Parent schema
var itemSchema = new Schema({
lsin: Number,
identifier: {
isbn13: Number,
},
title: snippetSchema,
});
Which upon Item.find returns an object like so:
[
{
_id: (...),
lsin: 676765,
identifier: {
isbn13: 8797734598763
},
title: {
_id: (...),
snippet: [
{
language: 'se',
text: 'Pippi Långstrump'
}
]
}
}
]
I would like to skip one nested level of the subdocument when the object is returned to the client:
[
{
_id: (...),
lsin: 676765,
identifier: {
isbn13: 8797734598763
},
title: {
language: 'se',
text: 'Pippi Långstrump'
}
}
]
So far I have tried:
#1 using a getter
function getter() {
return this.title.snippet[0];
}
var itemSchema = new Schema({
...
title: { type: snippetSchema, get: getter }
});
But it creates an infinite loop resulting in RangeError: Maximum call stack size exceeded.
#2 using a virtual attribute
var itemSchema = new Schema({
..., {
toObject: {
virtuals: true
}
});
itemSchema
.virtual('title2')
.get(function () {
return this.title.snippet[0];
});
Which will generate the desired nested level but under a new attribute, which is not acceptable. To my knowledge there is no way of overriding an attribute with an virtual attribute.
The question is: is there any other way to go about in getting the desired output? There will be several references to the snippetSchema across the application and a DRY method is preferred.
I am new to MongoDB and Mongoose.
You'll need to use the $project within an mongodb aggregation pipeline.
Within my database I have the following:
> db.items.find().pretty()
{
"_id" : 123,
"lsin" : 676765,
"identifier" : {
"isbn13" : 8797734598763
},
"title" : {
"_id" : 456,
"snippet" : [
{
"language" : "se",
"text" : "Pippi Långstrump"
}
]
}
}
Then we just need to create a simple aggregation query:
db.items.aggregate([
{$project: { lsin: 1, identifier: 1, title: { $arrayElemAt: [ '$title.snippet', 0 ] }}}
])
This just uses a $project (https://docs.mongodb.com/v3.2/reference/operator/aggregation/project/) and a $arrayElemAt (https://docs.mongodb.com/v3.2/reference/operator/aggregation/arrayElemAt/) to project the first item out of the array. If we execute that we will get the following:
{
"_id" : 123,
"lsin" : 676765,
"identifier" : {
"isbn13" : 8797734598763
},
"title" : {
"language" : "se",
"text" : "Pippi Långstrump"
}
}

MongoDB updating embedded document isn't working

I'm trying to update embedded document, but it is not working. This is what documents look like:
{
"_id" : ObjectId("577c71735d35de6371388efc"),
"category" : "A",
"title" : "Test",
"content" : "Test",
"tags" : "test",
"comments" : [
{
"_id" : ObjectId("57811681010bd12923eda0ca"),
"author" : "creator",
"email" : "creator#example.com",
"text" : "helloworld!"
},
{
"_id" : ObjectId("57811b17b667676126bde94e"),
"author" : "creator",
"email" : "creator#example.com",
"text" : "helloworld2!"
}
],
"createdAt" : ...,
"updatedAt" : ...
}
you can see the comments field is embedded document that contains comments. I want to update specific comment, so I made query like this(node.js):
db.update('posts', {
_id: new ObjectID(postId), // ID of the post
comments: {
$elemMatch: {
_id: new ObjectId(commentId)
}
}
}, {
$set: {
"comments.$.author": newComment.author,
"comments.$.email": newComment.email,
"comments.$.text": newComment.text,
"comments.$.updatedAt": new Date()
}
}) ...
when I run this query, no error was shown but update wasn't applied. I tried this query too:
{
_id: new ObjectId(postId),
"comments._id": new ObjectId(commentId)
}
but not worked either. Am I missing something? I'm using Mongo v3.2.7.
Please try the below code. I think the "ObjectId" (i.e. case) should be the problem. Just check how you defined the object id and keep it consistent in the two places that you have used (i.e. posts _id and comments _id -> both places).
ObjectID = require('mongodb').ObjectID
The below code works fine for me. Basically, your query seems to be correct.
var Db = require('mongodb').Db, MongoClient = require('mongodb').MongoClient, Server = require('mongodb').Server, ReplSetServers = require('mongodb').ReplSetServers, ObjectID = require('mongodb').ObjectID, Binary = require('mongodb').Binary, GridStore = require('mongodb').GridStore, Grid = require('mongodb').Grid, Code = require('mongodb').Code, assert = require('assert');
var db = new Db('localhost', new Server('localhost', 27017));
db.open(function(err, db) {
var collection = db.collection("posts");
var postId = '577c71735d35de6371388efc';
var commentId = '57811681010bd12923eda0ca';
var query = {
_id : new ObjectID(postId),
comments : {
$elemMatch : {
_id : new ObjectID(commentId)
}
}
};
collection.update(query, {
$set : {
"comments.$.author" : "new author",
"comments.$.email" : "newemail#gmail.com",
"comments.$.text" : "new email updated",
"comments.$.updatedAt" : new Date()
}
}, {
multi : false
}, function(err, item) {
assert.equal(null, err);
console.log("comments updated ..." + JSON.stringify(item));
});
});

mongojs replacing document with update command

With mongojs we need to update the document with something like below given code
db.data.update(
{
"title": {$regex : '.*Green Red.*', $options : 's'},
"editor.key": {"$in": ["74014","45339"]},`enter code here`
"types" : "Notes"
},
{
$set: {
"editor.key": "05335",
"editor.value": "editor1",
"editor.email": "editor1#gmail.com"
}
},
false,
true
);
But the problem is its really not dynamic
exports.updatePerson = function(Person , onDone) {
Person.UpdatedOn = new Date();
db.people.save(nodd, function (err) {
onDone();
});
But its creating a duplicate record
try this :
db.data.update(
{
"title": {$regex : '.*Green Red.*', $options : 's'},
"editor.key": {"$in": ["74014","45339"]},`enter code here`
"types" : "Notes"
},
{
$set: {
"editor.key": "05335",
"editor.value": "editor1",
"editor.email": "editor1#gmail.com"
}
},{
insert:false,
multi : true
}
);
insert and multi parameter should be fields of an object

Resources