So I'm new to MongoDB and is doing some learning on FreeCodeCamp. There is a task where I need to insert a document to database, I successfully completed the task, but the document doesn't seem to reach the database (I've checked it in MongoDB Compass). Here is the code I've wrote:
let mongoose = require('mongoose');
mongoose.connect(process.env.MONGO_URI, { useNewUrlParser: true, useUnifiedTopology: true });
let personSchema = new mongoose.Schema({
name: {
type: String,
required: true
},
age: Number,
favoriteFoods: [String]
}) // making schema for person
let Person = mongoose.model('Person', personSchema) // defining schema model for Person
const createAndSavePerson = (done) => {
var rickSanchez = new Person({
name: 'Rick',
age: 22,
favoriteFoods: ['Ice Cream', 'Nasi Kebuli']
}) // making new document from a model
rickSanchez.save(function(err, data) { // saving the document
if (err) return console.error(err)
done(null, data); // passing the document data to callback
})
};
I tried to call the function, but I got confused because there's a callback function as parameter, I tried to pass done() while calling it but it gets an error. What should I do so my document (rickSanchez) inside the function is being sent to MongoDB?
Any helpful answer would be very appreciated!
I'm trying to access the array of Message Models that is stored in my Conversation Model. However, when I use the populate method to try to store the Message models as an array, only the first Message is showing up.
socket.on('connected', function (data) {
//load all messages
const filter = { roomId: data.roomid };
(async () => {
console.log('searching for Schema');
let conversation = await Conversation.findOne(filter)
.populate('messages')
.exec(function (err, message) {
if (err) console.log('no schema found');
var array = message.messages;
console.log(array);
// printing only first Message
});
})();
});
Conversation Schema
const ConversationSchema = new mongoose.Schema(
{
roomId: {
type: String,
required: true
},
messages: {
type: mongoose.Schema.Types.ObjectId, ref: 'Message'
}
},
{
timestamps: true
}
);
populate method not store message as an array.Population is the process of automatically replacing the specified paths in the document with document(s) from other collection(s).Refer this for more detail
To solve your problem modify declaration of messages field in Conversation Schema
messages: [{
type: mongoose.Schema.Types.ObjectId, ref: 'Message'
}]
I'm quite newb to mongodb and mongoose, so I ask you to help me.
I've an API, which works and now I want to extend it with filtering for given params.
I've a Order model, which points to two different collections of documents Material & User Schemas and have a quantity element.
let Order = new Schema({
materials:
{
type: Array,
material: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Material'
},
qty: {
type: Number
}
},
userId: {
type: Schema.Types.ObjectId,
ref: 'User'
}
}, {
collection: 'orders'
})
Also I've method to create an order:
exports.createOrder = (req, res) => {
if (!req.body.user) {
res.status(400).send({message: 'Content can not be empty!'});
}
const order = new Order({
materials: req.body.materials,
userId: req.body.user
});
order
.save(order)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Order."
});
});
}
If I create Order filling only Material ID, it creates and filtering by given material ID in filter request.
post request
filter request
But If I trying to point qty it isn't present in response.
post request with qty
filter request ending with previous document id
There is my question: How can I create Order exact way I need (Material ID and qty number must persist) and How can I perform a filtering operations on them?
Any help appriciated.
My mistake was in method how I create order as well as I make a filtering request.
Correct method to create order with data storing in array type is following
exports.createOrder = (req, res) => {
if (!req.body.user) {
res.status(400).send({message: 'Content can not be empty!'});
}
const order = new Order({
materials: {material: req.body.materials, qty: req.body.qty},
userId: req.body.user
});
order
.save(order)
.then(data => {
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while creating the Order."
});
});
}
as you can see, difference is how I form materials array.
next thing is in filter request
exports.filterOrder = (req, res) => {
Order.find({"materials.material": req.body.material})
.then(data => {
console.log(data);
res.send(data);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Orders."
});
});
}
If I need to filter orders contain necessary material I need to place subelement of array in quotes with dot notation. This will work also with "material.qty" parameter if needed.
I'm very new to JavaScript and Mongoose. I'm building a small project using express, mongoose and node.js.
I have a mongoose model - Client that has an Array of Transactions
var Client = mongoose.model('Client', {
name: {
type: String,
required: true,
minlength: 1
},
email: {
type: String
},
phone: {
type: Number
},
createdAt: {
type: Number,
default: null
},
transactions: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Transaction' }],
_creator: {
type: mongoose.Schema.Types.ObjectId,
required: true
}
});
module.exports = {Client};
This is Transaction model:
var Client = require('./client');
var Transaction = mongoose.model('Transaction',{
_creator : { type: mongoose.Schema.Types.ObjectId, ref: 'Client' },
amount : {type: Number, min: 0},
date : {type: Number,default: null},
total: {type: Number,default: null}
});
module.exports = {Transaction};
When I POST a new Transaction it goes through and saves in db:
app.post('/clients/:id/transactions', authenticate, (req, res) => {
var id = req.params.id;
var transaction = new Transaction({
amount: req.body.amount,
date: new Date().getTime(),
total: req.body.total,
_creator: req.params.id
})
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}
transaction.save().then((doc) => {
Client.findOneAndUpdate({
_id: id,
_creator: req.user._id,
transactions: req.body.transaction
});
res.send(doc);
}, (e) => {
res.status(400).send(e);
});
});
I am also able to GET all the transactions associated with the client:
app.get('/clients/:id/transactions', authenticate, (req, res) => {
var id = req.params.id;
if (!ObjectID.isValid(id)) {
return res.status(404).send();
}
Transaction.find({
_creator: id
}).then((transactions) => {
res.send({transactions});
}).catch((e) => {
res.status(400).send();
});
});
But when I make a GET call to '/clients' - Array of Transactions is empty:
{
"clients": [
{
"_id": "1095d6de3867001108b803",
"name": "Peter",
"email": "peter#gmail.com",
"phone": 1232321,
"_creator": "5321df6d57868ec7001108b801",
"__v": 0,
"transactions": [],
"createdAt": null
} ]
}
And this is the GET call to /clients
app.get('/clients', authenticate, (req, res) => {
Client.find({
_creator: req.user._id,
})
.populate('transactions.transaction')
.then((clients) => {
res.send({clients});
}, (e) => {
res.status(400).send(e);
console.log('Unable to get clients', e);
})
});
I know that I'm likely doing something completely wrong but I don't know where I need to look for my mistake. Please help!
I would check if the client exist before adding a transaction. A transaction needs a client first.
Forewarn, I'm not a fan of then and catch so this answer does not use it. I normally use async.js when dealing with multiple asynchronous operations.
Anyways, I would do it like
app.post('/clients/:id/transactions', authenticate, (req, res) => {
Client.findOne({ _id: req.params.id }, (err, client) => {
if (err)
return res.status(400).send(err);
if (!client)
return res.status(400).send(new Error('No client'));
Transaction.create({
amount: req.body.amount,
date: new Date(), // I don't think you need .getTime()
total: req.body.total,
_creator: client._id
}, (err, transaction) => {
if (err)
return res.status(400).send(err);
client.transactions.push(transaction._id);
client.save(err => {
if (err)
return res.status(400).send(err);
res.json(transaction);
});
});
});
});
Good idea to also turn on debugging mode to see your queries: mongoose.set('debug', true).
You might also find using timestamps option for Transaction schema more useful than explicitly using date field
To get clients with their transactions
app.get('/clients', authenticate, (req, res) => {
Client.find({ _creator: req.user._id }).populate('transactions').exec((err, clients) => {
if (err)
return res.status(400).send(err);
res.json(clients);
});
});
so first of all i don't exactly know what _creator key in Client model representing, it's probably user identifier who has some clients, but if I'm wrong please correct me.
Honestly I don't know why you are making two way document connection, (keeping client in transactions, and also keeping transactions in clients) in my opinion first option is better for mongodb and using that you can easily get transaction's list with find, or mongodb aggregation, but you can't get data using populate.
In second option you need to remember that one document could have maximum 16MB. And also keeping thousands of transactions in one array is not well for performance. Think about example that you have 5000 transaction and you want to show list with pagination (50 records per page), with array option you have to get whole document, and splice array to 50 records. In first option you could use mongodb skip and limit. Please think about it.
Returning to question, mistake you are doing is here:
transaction.save().then((doc) => {
Client.findOneAndUpdate({
_id: id,
_creator: req.user._id,
transactions: req.body.transaction
});
res.send(doc);
Here you don't exactly say how this document should have to updated about.
Mongoose in method findOneAndUpdate using mongodb findAndModify method. But params are used from update.
https://docs.mongodb.com/manual/reference/method/db.collection.update/
And also documentation says that you what params like:
Query#findOneAndUpdate([query], [doc], [options], [options.passRawResult], [options.strict], [callback])
So first query param is mongo query to find one document in database, next param is object with updating query, and after that you could send some additional options in third param. So your code should looks like this:
transaction.save().then((doc) => {
Client.findOneAndUpdate({
_id: id,
_creator: req.user._id,
}, {
$addToSet: {
transactions: doc._id,
}
});
res.send(doc);
You could use addToSet or push both are putting element into array, but addToSet avoiding duplicates in array. And as you se we push new transaction identifier into this array. And after all you only populate transaction key.
I hope I helped. If you have any questions please ask.
I am trying to use mongoose addToSet to add multiple ObjectIds to a sub-doc array. I've used a similar method for adding a single subdocument, but I'm trying to figure out how to add multiple subdocuments.
Project Model
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
const ProjectSchema = new Schema({
title: {
type: String
},
files: [{
type: Schema.ObjectId,
ref: 'File'
}]
});
export default mongoose.model('Project', ProjectSchema);
File Model
import mongoose from 'mongoose';
var Schema = mongoose.Schema;
const FileSchema = new Schema({
fileUrl: {
type: String
},
date: {
type: Date
}
});
export default mongoose.model('File', FileSchema);
Controller
Project.create({fileUrl: req.fileUrl}, (err, proj) => {
if (err) {
console.log(err);
return res.status(400).send(err);
} else {
File.distinct('_id', {date: {"$lt": req.date }}).exec((err, files) => {
if (err)
return (err)
var added = new File([files]) <-----THE PROBLEM
proj.files.addToSet(added)
proj.save()
return res.status(200).send('OK');
})
}
});
//Usually I would do something like this if I were adding one subdocument to an array:
(example)
var foo = new File(file)
proj.files.addToSet(foo)
proj.save()
You are very close! When you're passing the added variable, you are not sending the list of values through. You are basically sending a single object of arrays.
If you'd like to perform the addToSet function, try doing a simple iteration:
files.forEach(function(f) {
var added = new File(f)
proj.files.addToSet(added);
proj.save(function(err, saved){
if (err)
throw (err)
return;
})
});
You can find more examples of javascript iterators here:For-each over an array in JavaScript? There is an amazing and comprehensive list of options for you.