I am new to Mongodb so below code i am trying to update document field string i have object recieved in post req.body now based on _id i want to update string field but it is not updating record with below implementation. How can i update record using _id ? Any better approach to update record in terms of async will be appreciated.
routes.js
var Diagram = require('./diagram');
router.post('/saveUpdateDiagram',function(req,res){
console.log(req.body._id);
Diagram.update(req.body);
});
diagram.js
var diagram = require('./diagram.model');
var mongoose = require('mongoose');
var Diagram = {
index: function(callback) {
diagram.find({}, function(err, result) {
if (!err) {
callback(result);
}
});
},
update: function(data) {
console.log('data in controller', data);
Diagram.update({ $set: { 'string' : data.string } });
}
}
module.exports = Diagram;
data.json
{
_id: "57fe42efc3590c7686bad563"
groups: Array[0]
owner: "sh587"
string: "test string should be updated"
text: "gcs_wf_dit.bpmn"
users: Array[1]
}
We know that JavaScript is case sensitive language you should use diagram.update not Diagram.update
use `diagram.update({ $set: { 'string' : data.string } });
diagram.js should be
diagram.js
var diagram = require('./diagram.model');
var mongoose = require('mongoose');
var Diagram = {
index: function(callback) {
diagram.find({}, function(err, result) {
if (!err) {
callback(result);
}
});
},
update: function(data) {
console.log('data in controller', data);
diagram.update({_id: data._id}, { $set: { 'string' : data.string } });
}
}
module.exports = Diagram;
Make sure you have "string" field in diagram model. as mongoose allow only those fields which are available in schema. If not then add
string: {type: String}
and then use
var diagram = require('./diagram.model');
var mongoose = require('mongoose');
var Diagram = {
index: function(callback) {
diagram.find({}, function(err, result) {
if (!err) {
callback(result);
}
});
},
update: function(data) {
diagram.update({_id: data._id}, { $set: { 'string' : data.string } });
}
}
module.exports = Diagram;
Related
I want to search my User collection via full text search on both the firstName and lastName property.
I create my index like this:
//connect to connector
server.datasources.db.connector.connect(function (err, db) {
//create index on firstName and lastName
db.collection('User').createIndex(
{ firstName: "text", lastName: "text" },
function (err) {
if (!err) {
//run autoupdate on each model
Promise.each(server.models(), function (model) {
if (model.dataSource) {
var autoupdate = Promise.promisify(model.dataSource.autoupdate);
if (autoupdate) {
console.log("running autoupdate on model: " + model.modelName);
return autoupdate.call(model.dataSource, model.modelName);
}
}
});
}
else {
console.log(err);
}
});
});
But when i query the database for a search string i always get zero results via direct access:
var mongodb = models.User.getDataSource().connector.collection(models.User.modelName);
mongodb.find({ $text: { $search: "Test" } },
function (err, resp) {
resp.toArray(function (err, docs) {
//docs always empty
cb(null, docs);
});
});
Or i get every entry when i use the Angular client like:
User.find({ where: { '$text': { search: "Test" } } }).$promise.then(function (resp) {
console.log(resp)
});
What am i missing?
I'm trying to get an item from my DynamoDB but get the following error
ValidationException: The provided key element does not match the
schema
The create item piece of the code works. But no the Get item.
Table Info:
Table Name: movieTable
Primary Partition Key: itemID
Primary Sort Key: sortKey
Here's the code for the create and update:
var fbUserId;
var params;
var keyText;
var attText;
var valText;
var dynamodb = null;
var docClient = null;
var appId = '405140756489952'; //from facebook
var roleArn = 'arn:aws:iam::042765862882:role/Verzosa'; //from AWS IAM
var resultData = null;
document.getElementById('putThis').onclick = function () {
dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
keyText = document.getElementById("keyValue").value;
attText = document.getElementById("attributeText").value;
valText = document.getElementById("valueText").value;
console.log("Key Value: ", keyText);
console.log("Attribute: ", attText);
console.log("Value: ", valText);
params = {
TableName: 'movieTable',
Item: {
itemID: keyText,
sortKey: valText
}
};
docClient.put(params, function(err, data){
if (err) console.log(err);
else
{
resultData = data;
console.log(resultData);
}
})
};
document.getElementById('getThis').onclick = function () {
dynamodb = new AWS.DynamoDB({ region: 'us-west-2' });
docClient = new AWS.DynamoDB.DocumentClient({ service: dynamodb });
keyText = document.getElementById("keyValue").value;
attText = document.getElementById("attributeText").value;
console.log("Key Value: ", keyText);
console.log("Attribute: ", attText);
params = {
TableName: 'movieTable',
Key: {
itemID: keyText,
},
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText
}
};
docClient.get(params, function (err, data)
{
if (err)
{
console.log(err, err.stack);
}
else
{
console.log("success, logging data: ");
console.log(data);//shows keys
console.log("attribute 1 is " + data.Item.sortKey)
//var output = data.Item.attribute1;
l = document.getElementById("output");
l.innerHTML = data.Item.sortKey;
}
})
};
Any help would be appreciated.
You are getting this error because when using AWS.DynamoDB.DocumentClient.get method, you must specify both hash and sort key of an item. But you have only hash key specified (itemId), and sort key is missing.
Here is how your get params should look like:
...
params = {
TableName: 'movieTable',
Key: {
itemID: keyText,
sortKey: valText // <--- sort key added
},
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText
}
};
docClient.get(params, function (err, data) {
...
If you'd like to get a record with a hash key only, without specifying its sort key, you should use query method instead of get:
...
params = {
TableName: 'movieTable',
KeyConditionExpression: '#itemID = :itemID',
ProjectionExpression: "#a",
ExpressionAttributeNames: {
'#a': attText,
'#itemID': 'itemID'
},
ExpressionAttributeValues: {
':itemID': keyText
}
};
dynamodbDoc.query(params, function(err, data) {
...
Be aware that while get method always returns 1 or no records, query can possibly return multiple records, so you would have to revisit your current implementation of get callback (e.g. instead of accessing data.Item you should use data.Items array, see query method docs)
You need to pass both primary key and sort key in the params.
You can find these keys from the table UI.
and you should pass those as parameters when making the request
this.ProjectsModel.delete({pk1:"project#giri-test#appsc", sk1:"metadata#giri-test#appsc"}, (error) => {
if (error) {
console.error(error);
} else {
console.log("Successfully deleted item");
}
});
I have a NodeJS application which is using mongoosastic api for Elasticsearch. Now I need a way of adding a condition(isDeleted == false) to the specified query(query: req.query.q).
this is my code
exports.search = function (req, res) {
if (!req.query.q) return res.status(400).send('No Query Provided')
log.error(req.query.q)
User.search({query_string: {query: req.query.q}}, (err, results) => {
if (err) return handleError(res, err)
var ret = _.map(results.hits.hits, result => ({
userID: result._id,
_score: result._score,
name: result._source.name,
loc: result._source.loc,
info: result._source.info,
images: result._source.images
}))
return res.send(ret)
})
}
How can I do that?
You can use filtering to filter your data. Check the example of your plugin website. Following example is your answer also.
var geoQuery = {query_string: {query: req.query.q}};
var myFilter = {
term: {
isDeleted: true
}
};
User.search(geoQuery, {filter: myFilter}, function(err, res) { /* ... */ })
I want to create using mongoose js a collection of kitten with this document in it {name: "mike"}.
After creating this document I want to print it's value.
I wrote this code below.
2 problems:
this code doesn't end (meaning when I wrote node file.js the cmd line stays open (stucked) and no return value is return (infinite loop like in a server).
the code doesn't print the value of "mike". just create this doucument...
what am I doing wrong?
thanks
var mongoose = require('mongoose');
var url = 'mongodb://Yotam:Yotam#ds023475.mlab.com:23475/small-talkz';
mongoose.connect(url);
var kittySchema = mongoose.Schema({
name: String
});
var Kitten = mongoose.model('kitten', kittySchema);
Kitten.create({ name: "mike" }, function (err, small) {
if (err) return handleError(err);
});
Kitten.findOne( { } ), function(err, docs){
console.log(docs.name);
};
return 1;
newKitten = { name: "mike" };
Kitten.create(newKitten, function (err, kitty) {
if {
(err) return handleError(err);
} else {
console.log(kitty); //OR console.log(kitty.name);
}
});
Kitten.findOne({name: "mike"}).exec(function(e, kitten) {
if (e) {
console.log(e)
} else {
console.log(kitten.name)
}
});
the problem was {for anyone whose intersted (and thanks for herkou)} that I did not use the exec command..
This works:
Kitten.findOne( { name: "mike"} ).exec( function(err, docs){
console.log(docs.name);
return;
});
update:
also had a probelm with race conditions... the create of the documnet not finished when the query was called. that is why I got undeinfed.
use this new code:
var mongoose = require('mongoose');
var url = 'mongodb://Yotam:Yotam#ds023475.mlab.com:23475/small-talkz';
mongoose.connect(url);
var kittySchema = mongoose.Schema({
name: String,
color:String
});
var Kitten = mongoose.model('Kitten', kittySchema);
var newKitten = { name: "mike", color:"white" };
Kitten.create(newKitten, function (err, kitty) {
if (err) {
return handleError(err);
} else {
call_query();
}
});
var call_query= function(){
var query= Kitten.findOne( { name: "mike"} );
query.exec( function(err, docs){
console.log(docs.color);
return;
});
}
return 1;
now I just need to understand why this script doesn't end.
I am trying to take advantage of the Waterline ORM in Sails.js to build an example app that has a model called 'Category'. Because a category can have multiple sub categories, I have the following one-to-many association for this model:
module.exports = {
adapter: 'mongo',
// adapter: 'someMysqlServer',
attributes: {
categoryTitle: {
type: 'string',
required: true
},
parentCat: {
model: 'category'
},
subCategories: {
collection: 'category',
via: 'parentCat'
},
articles: {
collection: 'article',
via: 'category',
required: false
}
}
};
In the CategoryController.js, I have the create method that first tries to see if the new category has a parent category assigned to it; however, I feel the code is quite messy, and the parentCat in Mongodb is always empty even if I tried to assign a parent category in the form submission. So I am wondering if this is the right way to do it:
create: function(req, res, next) {
var params = req.allParams();
// set parent category if exists
if (params.parentCat) {
Category.findOne({categoryTitle : params.parentCat})
.exec(function(err, category) {
if (err) {
return false; //not found
} else {
params.parentCat = category.id; //found the parent category
console.log('parent cat id is: ', category.id);
}
});
}
Category.create(params, function(err, newCategory) {
if (err) {
return next(err);
} else {
console.log('new category created');
}
console.log('successfully added the category: ' + newCategory.categoryTitle)
res.redirect('/category');
}); // create the category
}
The issue of your code is the callback.
I created a new version of code with the async feature (which is already in your sails app), hope it will help you.
create: function(req, res, next) {
var params = req.allParams();
async.waterfall([
function(callback) {
// set parent category if exists
if (params.parentCat) {
Category.findOne({
categoryTitle: params.parentCat
})
.exec(function(err, category) {
if (err) {
return false; //not found
}
params.parentCat = category.id; //found the parent category
console.log('parent cat id is: ', category.id);
callback(null, params);
});
} else {
callback(null, params);
}
},
function(params, callback) {
Category.create(params, function(err, newCategory) {
if (err) {
return next(err);
}
console.log('successfully added the category: ' + newCategory.categoryTitle);
callback(null, newCategory);
}); // create the category
}
], function(err, result) {
console.dir(result);
res.redirect('/category');
});
}