Dynamodb: unable to access nested objects and array of objects - node.js

I am new to DynamoDb. I am trying to access an object inside the array:
Created a new item in a table-
survey.create({
survey_name: 'Cycle',
description: 'Describe me',
test:[{
title:'hello1'
},{
title:'hello2'
}]
}, function (err, survey) {
if(err){
console.log(err)
}else{
console.log('created', survey.get('survey_name'));
}
});
I am not to able to fetch "test[n].title", getting 0 results.
survey.query('Cycle')
.filter('test.title').equals('hello2') //Tried it with test[0].title also
.exec((err,data)=>{
if(err){
console.log(err);
}
else{
console.log(data);
}
});
Also, I want to retrieve a part(json) of the item of a table ie. 'test' if its possible

Querying DynamoDB using filters requires the key you are filtering on to be the top level key. You cannot filter using a nested object key.

Related

Modify data coming from MongoDB query in Node.js

I have a simple query that fetches data from MongoDB. The query returns an array of objects in which the seller ID matches. I want to change a particular field in all of the returned objects. Is there a more efficient way than to use map?
I have added the query here:
Order.find({ sellerId: sId })
.populate("address")
.exec((err, result) => {
if (err) {
return res.status(403).json({
error: err,
});
}
return res.status(200).send(result);
});
And this is the API response:
[
{
"orderDate": "2022-04-21T13:16:22.198Z",
"sellerId": "6212072976836b7c70d3dcb1",
"__v": 0
}
]
I want to change order date for each object in the object of arrays returned.

How can I filter arrays in mongodb using mongoose?

I have created database with two collections. Each of these collections connected with relations.
Here I want to pass one item _id and check whether it passed to other collection as foreign key. If it's passed, I want to filter all items which consist as _id. How can I do that. Here my mongoose query and screenshot of db. Thank you
route.get("/:id",(req,res)=>{
Vehicles.find({
categories: [req.params.id]
}, (err, data)=>{
if(err){
console.log(err);
}else{
console.log(data);
}
});
PS: For an example I want to get all vehicles which have category id "60c58c2dcf82de0780051378" inside categories array.
Following the mongo document, you can query for all documents where categories is an array that contains the objectId req.params.id as one of its elements.
Vehicles.find({
categories: req.params.id,
}, (err, data) => {
if (err) {
console.log(err);
} else {
console.log(data);
}
});

Mongoose insert empty Object in array

I am trying to update collection using mongoose with NodeJS .when I log data to NodeJs console it prints the data but not data is not being pushed in collection.
But when i run the same query on mongodb console then it works
Here's the code
portoflio is array of objects
portfolio_user={
'title' : 'this is the sample link',
'type" : 'link',
'url" : 'www.emumba.com'
}
var user_id=req.user._id;
console.log(user_id);
var portfolio_user=req.body;
console.log(portfolio_user)
User.update({_id:user_id},{
$push:{
"portfolio":portfolio_user
}
},
function(err, user){
if (err) {
console.log(err);
} else {
console.log(user);
res.jsonp(user);
}
});
Note: I have also tried findOneAndUpdate and findById but they still dont work

Selecting only modified subdocument from Mongo

I have a mongoose query like this:
var query = Events.findOneAndUpdate({ '_id': event._id,'participants._id':participant._id},{'$set': {'participants.$': participant}}, {upsert:false,new: true},function(err,result){
if(err){
return res.status(500).jsonp({
error: 'Unable to update participant'
});
}
console.log(result.participants[0]);
res.jsonp(result.participants[0]);
});
and the query works properly modifying the participants subdocument inside Events collection.
The problem:
I need only the modified participant to be returned as JSON and I am not in need of the entire participants array but I am not able to achieve this since I get all the participants when I do console.log(result.participants);
How do I get only the modified subdocument after the query?
You may have to use the native JS filter() method as in the following:
Events.findOneAndUpdate(
{ '_id': event._id, 'participants._id': participant._id },
{ '$set': { 'participants.$': participant } },
{ upsert: false, new: true },
function(err, result){
if(err){
return res.status(500).jsonp({
error: 'Unable to update participant'
});
}
var modified = result.participants.filter(function(p){
return p._id === participant._id
})[0];
console.log(modified);
res.jsonp(modified);
}
);

Waterline : Access populated value within a model

I am using sails.js to develop my first app. I have a waterline model as shown below.
//ModelA.js
module.exports = {
attributes: {
//more attributes
userId: {
model: 'user'
},
//more attributes
}
};
I am using the model in one of my controllers as shown below.
ModelA.find(options)
.populate('userId')
.exec(function (err, modelA) {
//some logic
//modelA.userId is undefined here
res.json(modelA); //userId is populated in the JSON output
});
How do I get access to the populated value inside the model?
ModelA.find returns array of items.
ModelA.find(options)
.populate('userId')
.exec(function (err, results) {
console.log(results[0].userId) //results is an array.
//res.json(modelA);
});
Or you can use ModelA.findOne for a single record
It's because find return an array of records. You have to use index to access an object and then userId of that object.
ModelA.find(options).populate('userId').exec(function (err, recordsOfModelA) {
if(err) console.log(err);
else console.log(recordsOfModelA[0].userId)
});

Resources