MEAN Mongodb replace collection and create documents - angularjs

So i have a schema like that:
var article = new mongoose.Schema({
title : String,
comments : [{
pe: mongoose.Schema.Types.ObjectId,
ref: 'Comment'
}]
});
and:
var comment = new mongoose.Schema({
created : Date,
text : String
});
now i have my small angular application, when i retrieve with my API and article i get something like:
{
title : "please help me"
comments : []
}
now on the front end I push some comments and the new object is:
{
title : "please help me"
comments : [{
text : "Now, go f**k yourself",
date : "1 January 1970 00:00:00 UTC."
}]
}
when i call the API and I update the document, I would like that mongo would Create the sub-object comments by himself, is there a way ?
Should it do it automatically ?

I would write a article schema this way (disclaimer: I haven't tried mongoose):
var article = new mongoose.Schema({
_id: String // or mongodb.ObjectId? I'm not sure.
title : String,
comments : [{
_id: String // this field is not nessesary
text: String,
date: String
}]
});
var comment = new mongoose.Schema({
created : Date, // created or date?
text : String
});
When you retrieve with you api, you'll get something like:
{
"_id": "ndrjgnd..fesf",
"title": "grdgrdgr",
comments: [ {"text": "hello", "created": "2016:...:"}, ... , ]
}
Then you can push new comments from front end:
{
"articleId": "ndrjgnd..fesf", // this is important.
"text": "balabala"
}
And update db: db.article.update( {_id: ariticleId}, {$push: newComment})

Related

Join 2 collections in mongo with a where clause [duplicate]

I have a collection named Releases that holds a subdocument named product.
My collection looks like this:
{
"_id": ObjectId("5b1010e4ef2afa6e5edea0c2"),
"version": "abc",
"description": "<p>abc</p>\n",
"product": {
"_id": ObjectId("5b100c9949f43c6b6f10a93f"),
"name": "Product 1",
"description": "<p>abc</p>\r\n",
"image": "Product 1.png",
"__v": 0
},
"releasedate": ISODate("2018-05-30T00:00:00Z"),
"__v": 0
}
I am trying to find all releases associated to a specific product.
var id = req.body.productId
var query = {'product._id' : id};
Release.find(query)
.select('_id version description product releasedate')
.sort({releasedate: 1 })
.exec()
.then(releases => {
console.log(releases);
res.status(200).json(releases);
})
But it gives me an empty array if i console.log(releases)
I've been stuck with this for a while and asking u guys for help. What am i doing wrong. I read the documentation on https://docs.mongodb.com/manual/tutorial/query-embedded-documents/ and tried to apply that to my code but i cant get it to work.
My Schema looks like this:
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var Product = new Schema({
_id: Schema.Types.ObjectId,
name: String,
description: String,
image: String,
});
module.exports = mongoose.model('Product', Product);
You need to change your id from string to mongoose objectId
var id = req.body.productId
var query = {'product._id' : mongoose.Types.ObjectId(req.body.productId)};
Release.find(query)
.select('_id version description product releasedate')
.sort({releasedate: 1 })
.exec()
.then(releases => {
console.log(releases);
res.status(200).json(releases);
})

mongoose + nodejs push array to database

I'm new to NodeJS + Mongoose and having trouble pushing an array to my database via mongoose.
I have the following schema:
const StudentSchema = mongoose.Schema({
name: {
type: String
},
quizzes: [{
quiz: String,
answers: []
}]
});
What I'm trying to do is have an array of quiz objects in which the quiz number is shown and the array of answers for that particular quiz.
When I create a new user, the student object looks like this in mongoose:
{
"_id" : ObjectId(id here),
"name" : "John",
"quizzes" : [ ],
"__v" : 0
}
The function I'm using to update the quiz array:
module.exports.addQuiz = function(student_id, answers, callback){
Student.findByIdAndUpdate(
student_id,
{$push: {"quizzes": {answers: answers}}},
{safe: true, upsert: true},
function(err, model) {
console.log(err);
}
);
}
And this is my route which calls the function whenever the endpoint is hit with a student_id, which will then be used to find the student and push to the array
router.post('/quiz/:student_id', (req, res, next) => {
var student_id = req.params.id;
var answers = req.body.answers;
Student.addQuiz(student_id, answers, (err, answers) => {
//error handling
})
});
I'm trying to test this by sending a post request to /quiz/:student_id with an id of a student in my database with the following JSON sent in the body:
[{
"quiz": "Quiz 1",
"answers": ["Answer 1", "Answer 2"]
}]
Although when I try this it ends up getting hung somewhere and the request never completes - I also get a "null" in the console.
Can anyone help me out? Thank you.

Possible circular reference in mongoose

I have a problem with circular dependency when I need to SAVE a 'document'.
My application is all Restfull with the interface via AngularJS.
On the screen to create a COMPANY, you can create OBJECTS and SERVICES. In the creation of the SERVICES screen, it must associate a created OBJECT.
The problem is that the OBJECT created has not yet persisted, so I don't have a _id. Thus it is not possible to reference it in SERVICE. Only when I have an OBJECT persisted, I can associate it in company.services[0].object.
Any suggestion?
This is what I need to be saved in MongoDB. Look at the reference to the OBJECT "5779f75a27f9d259248211c7" in SERVICE.
/* 1 */
{
"_id" : ObjectId("5749bb92bf8145c97988e4a9"),
"name" : "company 1",
"services" : [
{
"_id" : ObjectId("5764cb2c00d00cf10c9c41c6"),
"description" : "service 1",
"object" : ObjectId("5779f75a27f9d259248211c7"),
}
],
"objects" : [
{
"_id" : ObjectId("5779f75a27f9d259248211c7"),
"description" : "object 1",
}
]
}
And this is my Schema:
var objectSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
unique: true,
required: 'Description is required'
}
})
var serviceSchema = new mongoose.Schema({
description: {
type: String,
trim: true,
required: 'Description is required'
},
object: {
type: mongoose.Schema.ObjectId,
ref: 'Object'
},
})
var companySchema = new mongoose.Schema({
name: {
type: String,
default: '',
trim: true,
unique: true,
required: 'Name is required'
},
guarda_documentos: {
services: [serviceSchema],
objects: [objectSchema],
},
});
mongoose.model('Company', companySchema);
mongoose.model('Object', objectSchema);
You would need to persist your object first and once this is done, use the returned _id to then persist your service. This is an async process.
// Simplified code concept
// Create a new object
var o = new Object()
o.description = 'foo'
...
// Create a new service
var s = new Service()
// Save object and return it (will now have an _id)
o.save(function(err, doc) {
// Add object reference to service and save
s.object = doc._id
s.save(function(err, res) {
// Call your controller or whatever callback here
})
})
On another note don't use "Object" as the name of your doc. It already defined in Javascript.

Embed the collections using mongodb

I am just start using mongodb and nodejs. I know that mongodb does not supports joins.I simply insert the data in mongodb and my documents looks:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"content": "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
Now user comments on this post. it should be like this:
{
"_id": ObjectId("564dacf84d52785c1d8b4567"),
"comments": [
{
"name": "sumit",
"email": "sumit#ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
}
],
"content"▼: "This blog created by karanSofat",
"html": "<p>This blog created by karanSofat</p>\n",
}
Here is my models,
//post model
// grab the mongoose module
var mongoose = require('mongoose');
// define our nerd model
// module.exports allows us to pass this to other files when it is called
module.exports = mongoose.model('post', {
content : {type : String, default: ''},
html : {type : String, default: ''}
});
//comment model
var mongoose = require('mongoos
module.exports = mongoose.model('comment', {
name : {type : String, default: ''},
email : {type : String, default: ''},
comment : {type : String, default: ''},
posted_at : {type : date, default: ''}
});
My Problem is that I don't know on which way I insert comments data using nodejs and my document will embed.
Here is my code:
app.post('/comments/:id', function(req, res) {
var Comment = require("../app/models/comments");//comment Model
var blog = require("../app/models/blog");//blog model
var id = req.params.id; //postId
var comments = JSON.parse(JSON.stringify(req.body)); //commentdata
//code Should be here
res.json({data:id,data2:input});
});
Please help
Karan,
Let's assume you have the following schema:
var Comments = new Schema({
name: String,
email: String,
comment: String,
, posted_at: Date
});
var BlogPost = new Schema({
content : String,
html : String,
comments : [Comments],
});
mongoose.model('BlogPost', BlogPost);
You can add an embed document to an array as such:
// retrieve my model
var BlogPost = mongoose.model('BlogPost');
// create a blog post
var post = new BlogPost();
// create a comment
post.comments.push({
"name": "sumit",
"email": "sumit#ggi.net",
"comment": "this is also well for me",
"posted_at": ISODate("2015-11-19T11:06:27.172Z")
});
post.save(function (err) {
if (!err) console.log('Success!');
});

How to get data from array in mongoose?

I am new to mongoose node.js and mongoDB, I have a db Schema like
Project:{
projectName:"String",
projectManager:"String",
task:[{
taskName:"String",
timetakeninhrs:"String"
}]
};
So what I want is to get only the details of task with particular task name.
I am writing sql script so that you can know what I want :
Select taskname,timetakeninhrs from project where taskName ='DB create';
The $elemMatch projection operator would come in handy for this:
Project
.where('task.taskName', 'DB create') // or where('task.taskName').equals('DB create').
.select({_id: 0, task: {$elemMatch: {'taskName': 'DB create'}})
.exec(function(err, docs){
var tasks = docs.map(function(doc){ return doc.task[0]; });
console.log(tasks[0].taskName); // 'DB create'
console.log(tasks[0].timetakeninhrs); // '3'
});
In the above, the where() method acts as a static helper method of the Mongoose model that builds up a query using chaining syntax, rather than specifying a JSON object. So
// instead of writing:
Project.find({ 'task.taskName': 'DB create' }, callback);
// you can instead write:
Project.where('task.taskName', 'DB create');
// or
Project.where('task.taskName').equals('DB create');
and then chain the select() method to project the 'task' array field using $elemMatch. In the exec() method (which executes the query asynchronously), you need to pass in a callback which follows the pattern callback(error, results). What results is depends on the operation: For findOne() it is a potentially-null single document, find() a list of documents, count() the number of documents, update() the number of documents affected, etc. In this case this returns an array of documents in the format:
[
/* 0 */
{
"task" : [
{
"taskName" : "DB create",
"timetakeninhrs" : "3"
}
]
},
/* 1 */
{
"task" : [
{
"taskName" : "DB create",
"timetakeninhrs" : "9"
}
]
}
/* etc */
]
In your callback you can do a bit of data manipulation to get an object that only has those properties you specified, hence the use of the native JavaScript map() function to create a new array of objects with those fields
i create this example that can help you:
var async=require('async');
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var uri = 'mongodb://localhost/myDb';
mongoose.connect(uri);
// define a schema
var ProjectSchema = new Schema({
projectName: "String",
projectManager: "String",
task: [{
taskName: "String",
timetakeninhrs: "String"
}]
});
// compile our model
var Project = mongoose.model('Project', ProjectSchema);
// create a documents
var Project01 = new Project({
projectName: "Project01",
projectManager: "Manager01",
task: [{
taskName: "tsk01_Project01",
timetakeninhrs: "1111-1111"
}, {
taskName: "tsk02_Project01",
timetakeninhrs: "1111-2222"
}, {
taskName: "tsk03_Project01",
timetakeninhrs: "1111-3333"
}, {
taskName: "tsk04_Project01",
timetakeninhrs: "1111-4444"
}]
});
var Project02 = new Project({
projectName: "Project02",
projectManager: "Manager02",
task: [{
taskName: "tsk01_Project02",
timetakeninhrs: "2222-1111"
}, {
taskName: "tsk02_Project02",
timetakeninhrs: "2222-2222"
}, {
taskName: "tsk03_Project02",
timetakeninhrs: "2222-3333"
}, {
taskName: "tsk04_Project02",
timetakeninhrs: "2222-4444"
}]
});
//delete existing documents and create them again
Project.remove({}, function() {
Project01.save(function() {
Project02.save(function() {
//for example we find taskName: "tsk03_Project02"
Project.find({'task': {$elemMatch: {taskName: "tsk03_Project02"}}},'task.taskname task.timetakeninhrs',function(err, docs) {
if (!err) {
console.log(docs);
}
});
});
});
});

Resources