Search for user with Mongoose - mongodb

User's have accounts and can comment using a Comment Mongoose model.
Is it possible to allow a user to enter a username in an text input and generate all user associated comments?
I've been trying to use db.users.find({"name") but I'm not sure how to pass a username from an input field to search. Any suggestions?
Thanks! The comment model is below. It connects each comment with a user.
var commentSchema = new mongoose.Schema({
body: String,
author: {
id: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
username: String
}
})

You have to take the user name entered by another user from the text box and send it with the request to back end, something like {uName : 'user2'}.
Use the value from the object coming with the request and do find in your data base.
Something like below will be the backend code:-
var uName = req.params.uName
var cursor = db.users.find({username : uName.toString()});
Now cursor will give you record from user2 from your data base.

Front-end
send your GET request with Query String to the Back-end API
<form method="get" action="/comments/user" >
<input type="text" name="username" placeholder="username">
<button type="submit">Search</button>
</form>
Back-end
write a API to deal with GET request and Mongoose Query
var app = express();
var Comment = mongoose.model('Comment', commentSchema);
app.get('/comments/user', function (req, res) {
var query=Comment.find();
//get the Query String here
var filter=req.params.username;
if(filter.length>0){
query.where({author.username:filter});
}
query.exec(function (error, comment) {
//send the result back to front-end
res.json({ Comment: comment });
});
});

Related

update specific field in meteor

I want to update only the name field, the problem with the code I have is that if I update a document, all the mongo documents are updated.
As I update a document in specific, I must admit that I am new to this mongo so any help I thank you.
Client
updatename.html
<template name="updatename">
<form class="editor-container">
<input class=“save” type="text" id="card" value=“{{name}}”>
<button type="button" class=“save” id="save">save</button>
</form>
</template>
updatename.js
Template.updatename.events({
'click .save’: function (e, t) {
e.preventDefault();
FlowRouter.watchPathChange();
var name = FlowRouter.current().params._id;
var name = $('#card').val();
Meteor.call('name.update',name);
FlowRouter.go('/');
}
});
Server
name.js
Meteor.methods({
'name.update'( name) {
Name.update({
Name.update({},{ $set: { nam: name }},{multi:true})
});
}
});
I recommend few improvisations over your Meteor code.
Atleast use Title Case/ CamelCase for better readability of the template name and Meteor Methods for other developer.
use submit .formClassName instead of using click .save, also specifiy parameter name with sincerity like function (event, template)
When you updating document for logged user and not other user, as dmayo mentioned in the code use Name.update({_id: Meteor.userId()},{ $set: {nam: name}}), but there is no sense of specifying { multi: true } when you know that there is going to be only 1 record when you are updating. You can use { multi: true } when you desire to impact many records based on criteria that are definitely going to return more than 1 record.
use check(name, String) in Meteor.method call to ensure what you are sending to the server is eligible for further operations.
Use aldeed autoforms when you know there is no out of the box implementation and is going to be simple.
Below is the improvised code for better readability and up to standards
Client
update-name.html
<template name="UpdateName">
<form class="editorContainerForm">
<input type="text" id="card" value=“{{name}}”>
<button type="submit">Save</button>
</form>
</template>
update-name.js
Template.UpdateName.events({
'submit .editorContainerForm’: function (event, template) {
event.preventDefault();
FlowRouter.watchPathChange();
var name = FlowRouter.current().params._id;
var name = $('#card').val();
Meteor.call('updateName',name, function(error, response){
if(error){
// Show some BERT ERROR message for better user experience
// use "meteor add themeteorchef:bert" to add package
} else {
// Show some BERT SUCCESS message for better user experience
});
FlowRouter.go('/');
}
});
Server
name.js
Meteor.methods({
updateName( name ) {
check(name, String);
Name.update({ _id: Meteor.userId },{ $set: { name: name }});
// Use below code only if you know there can be multiple records for same ID
// Name.update({ _id: Meteor.userId },{ $set: { name: name }}, { multi: true });
}
});
In your name.js file (on the server) your mongo query is empty, so when mongo queries your database, it matches all of the documents/records.
Name.update(query, changes, options)
That is the format per the mongo docs. You need to have a unique identifier. Your form is saving a "name", and that's what you are passing to the Meteor.method, but you're not telling the method who's changing their name. If the user is logged in, then you can just use the meteor unique id Meteor.userId()
Name.update({_id: Meteor.userId()},{ $set: {nam: name}},{multi:true})
Also, your option multi:true says to update any and all documents that match the query. If in your original method as written, you had multi:false (the default) then only one document would have been updated (but probably not the one you wanted as the first match would have been updated because of your empty query field.
Mongo docs: https://docs.mongodb.com/manual/reference/method/db.collection.update/
Metor docs: https://docs.meteor.com/api/collections.html#Mongo-Collection-update

inserting in mongo using express

i am trying to put data in the mongodb using express but it is storing blank always ...also it is not printing any console logs :
the url i am hitting after starting the sever is
http://localhost:3000/posts?title=test&link=http://test.com
and it is showing the below output:
{"__v":0,"_id":"562717b064002b1c2e697b33","comments":[],"upvotes":0}
router.get('/posts', function(req, res, next) {
console.log('reached ere '+req);
var post = new Post(req.body);
post.save(function(err, post){
if(err){ return next(err); }
res.json(post);
});
});
Post Scheme:
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: {type: Number, default: 0},
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
mongoose.model('Post', PostSchema);
You are calling your API as
http://localhost:3000/posts?title=test&link=http://test.com
which will send title and link to server as query parameters and not body parameters. So your req.body in this case would be an empty object. That is the reason no data in being saved in your posts collection.
You have two options here:
Change your API to save req.query in posts collection which you can do as follows:
Replace
var post = new Post(req.body);
with
var post = new Post(req.query);
Pass link and title as body parameters instead of query parameters.

Mongoose: How can I access a select:false property in a schema method?

Quick code:
var userSchema = new mongoose.Schema({
username: String,
password: {type: String, select: false}
});
userSchema.methods.checkPassword = function(password, done) {
console.log(password); // Password to check
console.log(this.password); // stored password
...
};
I don't want the password to be accessible by default, but I need a method to check against a user inputted password before authenticating the user. I know I can do a query to the DB to include these values, but I'm a bit lost on how I could access the hidden property on the schema method itself. this in the method itself is just the returned query, so it seems like it is inaccessible? Should I be doing the checkPassword() function elsewhere?
You can use select to select password in query. This is an example query.
User.findOne().select('password').exec(callback);
And this must be what you want to check password.
userSchema.methods.checkPassword = function(password, done) {
User.findOne({username: this.username}).select('password').exec(function (err, user) {
if (user.password == password)
return true;
else
return false;
});
}
I hope this might help you.
You can explicitly allow the password field (with {select:"false"}) to be returned in your find call with "+" operator before field e.g.:
User.findOne({}).select("+password") // "+" = allow select hidden field
A right way is writing the fields on method findOne. You can ask the fields that you want to return. In your case, it should be:
await User.findOne({ username: this.username }, 'password').exec();
Documentation:
mongoose.findOne
Above answers only show selection for a single property.
For multiple properties, syntax is this one:
await this.userModel
.findOne({ email }, { status: 1, firstName: 1, religion: 1 })
.exec();
This will return:
{
_id: new ObjectId("62de5a5158b809468b812345"),
status: 'Active',
firstName: 'John',
religion: 'Christian Orthodox'
}

filling in the initializing data in mongodb

I am using mongoose and setting up schema for users as follows:
var users=new mongoose.Schema({
username:String,
password:String,
type:String
});
var User=mongoose.model('user',users);
Now i need this to have a initial row with values as admin,admin,admin
How to do this?
I am using express server to verify a user like this
app.post("/verifyLogin",function(request,response){
var usr=request.body.username;
var pass=request.body.password;
//VERIFICATION FROM DATABASE CODE GOES HERE
});
P.S I am new to mongodb/document-based data storage. Until now i used to work with phpmyadmin and insert data directly from the interface provided.
WHAT I TRIED:
i made a temp post handler like this
app.post("/initialize/database",function(request,response){
var user=new userModel({username:'admin',password:'password', type:'admin'});
user.save(function(err){
if(!err){return console.log('created')}else{console.log(err)}
return response.send(user);
});
})
and then in the console did this:
jQuery.post('/initialize/database',function(data){console.log(data)})
Don't know whether this is the best approach or not :(
I find it easiest to enter initialization data from the terminal.
$ mongo
> use database-name
> db.users.insert({ username: 'admin', password: 'admin', type: 'admin' })
That would work for simply getting things going, but I forbid you to store plain-text passwords in the database when in production. To implement hashing and to use node instead of the terminal you could create a route for creating a new user instead. Something like:
app.post("/signup",function(request,response){
var usr = request.body.username,
pass = request.body.password;
somePasswordHashingAlgoritm(pass, function (hashedPassword) {
User.insert({
username: usr,
password: hashedPassword,
type: 'admin'
});
});
});
Even better would be to implement a pre-save hook in mongoose, but that's the subject for another topic. If you don't want others to sign up to your site, just remove that code when you've created yourself.

Express/Node.js - How to save an array of objects within one Model [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Mongoose - Form to save model with embedded documents
I have a simple Mongoose schema with BlogPosts and Feeds embedded within those BlogPosts. So each BlogPost can have several Feed objects. Right now my HTML form allows me to correctly save one 'Feed' when I create a BlogPost, BUT I would like to be able to add many Feeds. How should I change my HTML/controller? Thanks much!
web.js
app.post('/blogpost/new', function(req, res){
var post = new BlogPost(req.body.post)
post.feeds.push(req.body.feed);
post.save(function() {
res.redirect('/blogposts');
});
});
/blogpost/new
<form method="post">
<input type="text" name="blogpost[title]"/>
<textarea name="feed[name]"></textarea>
<textarea name="feed[key]"></textarea>
<textarea name="feed[name]"></textarea>
<textarea name="feed[key]"></textarea>
</form>
schema
var Feed = new Schema({
name : { type: String }
, key : { type: String }
});
var BlogPost = new Schema({
title : { type: String, required: true, index: { unique: true } }
, feeds : [Feed]
});
How do I make it so that this form stores two 'Feeds' in the blogpost object it creates? right now it would save One Feed in the data with two name values and two key values.
Thanks much!
When you do something like this:
<textarea name="feed[name]"></textarea>
<textarea name="feed[key]"></textarea>
<textarea name="feed[name]"></textarea>
<textarea name="feed[key]"></textarea>
You are basically saying "I need a textbox for feed's name, feed's key, feed's name, feed's key which is obviously incorrect. If you did feed[0][name]...feed[1][name] that would say "i need a textbox for the first feed...i need a textbox for the second feed"
Since you are breaking the parent and child up, rather than handling them as sub objects, you'll need to push each into the blogPost.feeds and save it.
Just be careful with that approach, especially when editing, because you are simply adding new objects ontop of what could already be an existing array of feeds.