Need help creating mongoose query for special purposes - javascript

I got some problems with a certain mongoose query.
Imaging having a schema like so
var Task = new Schema({
title: String,
createdBy: {
type: Schema.Types.ObjectId, ref: 'User'
},
pool: [{
userId: {type: Schema.Types.ObjectId, ref: 'User'},
accepted: {
type: Boolean,
default: false
}
}]
});
Task.find({pool: {$elemMatch:{userId: userId, accepted: false}}}, {'pool.$':1}).populate('createdBy', '_id name surname').populate('pool.userId', '_id name surname').exec(function(err, tasks){
So now the thing is I really just get the array entry I want but i don't get the rest of the Document like title and createdBy.
Anyone any suggestions how to solve this?
Kind regards Thomas
Oh sorry my bad I don't have the keyword var in my schema. I was in such a hurry creating my question. So here again. I am using elemMatch to find a document by a criteria found in an Array. It works very well returning just the found array element. What I want thou is that it returns the whole document like 'title and createdBy and pool' but in pool just the one element matching the elemMatch criteria.
I hope my question is now more understandable.
Kind regards and thanks for your help

Remove the keyword var from title and createdBy.
Reason: Schema takes a JavaScript object as parameter and in defining a javaScript object, var should not be used in the properties.
//This is correct
{
prop: "Something"
}
//This is not
{
var prop: "Something"
}

Related

Mongoose create document in wrong key:value order according to schema

So I have a problem with understanding of how Mongo .create and .findAndUpdate operation works. I have mongoose 5.4.2X and a model with schema which has lot of key:value pairs (without any nested objects) in the exact order (in the code below I use 1. 2. 3. etc to show you the right order) like this:
let schema = new mongoose.Schema({
1.code: {
type: String,
required: true
},
2.realm: {
type: String,
required: true,
},
3.type: {
type: String,
required: true,
enum: ['D', 'M'],
},
4.open: Number,
5.open_size: Number,
6.key: typeof value,..
7...another one key: value like previous one,
8.VaR_size: Number,
9.date: {
type: Date,
default: Date.now,
required: true
}
});
and a class object which have absolutely the same properties in the same order like schema above.
When I form data for Mongo via const contract = new Class_name (data) and using console.log(contract) I have a necessary object with properties in the exact right order like:
Contract {1.code: XXX, 2.realm: YYY, 3.type: D, .... 8.VaR_size: 12, 9.date: 327438}
but when I'm trying to create/update document to the DB via findOneAndUpdate or (findByID) it writes in alphabetical order but not the necessary 1->9, for example:
_id:5ca3ed3f4a547d4e88ee55ec
1.code:"RVBD-02.J"
7.VaR:0.9
(not the 1->9)...:...
8.VaR_size:0.22
__v:0
5.avg:169921
The full code snippet for writing is:
let test = await contracts.findOneAndUpdate(
{
code: `${item_ticker}-${moment().subtract(1, 'day').format('DD.MMM')}` //how to find
},
contract, //document for writinng and options below
{
upsert : true,
new: true,
setDefaultsOnInsert: true,
runValidators: true,
lean: true
}
).exec();
Yes, I have read the mongoose docs and don't find any option param for
solving my problem or probably some optional params are matter but
there are no description for this.
It's not my first project, the funny thing is, that when I'm inserting
tons-of-docs via .insertMany docs are inserted according to schema
order (not alphabetical)
My only question is:
How do I fix it? Is there any option param or it's a part of findAnd....
operations? If there is not solution, what should I do if for me it's
necessary the right ordering and check existence of document before
inserting it?
Update #1 after some time I rephrase my search query for google and find a relevant question on SW here: MongoDB field order and document position change after update
Guess I found the right answer according to the link that I post earlier. So yes, it's part of MongoDB:
MongoDB allocates space for a new document based on a certain padding
factor. If your update increases the size of the document beyond the
size originally allocated the document will be moved to the end of the
collection. The same concept applies to fields in a document.
by #Bernie Hackett
But in this useful comment still no solution, right? Wrong. It seems that the only way to evade this situation is using additions optional params during Model.find stage. The same ordering using during project stage via .aggregate and it looks like this:
Model.find({query},{
"field_one":1,
"field_two":1,
.............
"field_last":1
});

Mongoose Schema extend timestamp to have new properties (username)

I´m using MongoDB and mongoose for a project that needs to track data creation and changes. The requirements are that I need to keep track of records creation and changes including the date/time and the application user (not the OS user) who did it.
I´ve seen the mongoose timestamps option that would solve my date/time issue, but is there a way to extend it to include extra properties, where I´m gonna be adding the application username ?
If not, is there a single place where I can write a function that will be called on every creation/update so that I can include/modify these fields ?
Today I´m insering these properties on every model like below, but I would like to move all of them to a single place.
var companySchema = mongoose.Schema({
name: {
type: String,
index: true
},
phone: {
type: String
},
deleted: {
type: Boolean
},
createdAt: {
type: Date
},
createdBy: {
type: String
},
updatedAt: {
type: Date
},
updatedBy: {
type: String
}
});
How would be the best approach for it ?
I would approach it by creating two models, one for each Data created, one for each Data Changes.
Data created which will have 6 fields one is createdBy, createdAt, and one will be a field with an array of reference id of Data Changes, deletedBy, deletedAt, deletedFlag.
Data Changes will have fields dataID which will have reference id of data created, updatedBy and updatedAt.
This way it will be easy to keep track of when it was created when it was changes and when it was deleted.
PS: You can remove either of Array in Data created model or dataID ref id in Data Change mode, it was just me being extra cautious while binding.

Mongo/MongooseJS Object model doesn't save correctly [duplicate]

I'm using Mongoose ODM to partially validate models before they are stored to MongoDB.
Is it possible to relax Mongoose schemata so that a given part of the document is not validated? I have tried to the following:
var MySchema = new Schema({
user_id: { type: Schema.ObjectId, ref: 'User' },
freeform_data: {},
});
For instance if I set the contents to:
{
user_id: '123456',
freeform_data: {
dataitem1: 'a',
dataitem2: 'b',
items: [ 1, 2, 3, 4 ]
}
}
Then only user_id is stored, which makes perfectly sense security-wise.
How can I disable mongoose's validation for this field?
I am using this application only for prototyping purposes so I don't care about security right now (I just want to prototype).
When you modify the contents of a Mixed field like freeform_data, you need to notify Mongoose that you've changed its value by calling markModified(path) on the modified document or a subsequent save() call won't save it.
For example:
user.freeform_data = { foo: 'bar' };
user.markModified('freeform_data');
user.save();
Mongeese
: a mongoose multi-database helper/hack module
https://github.com/donpark/mongeese
Disclaimer: I was looking to connect to two MongoDB instances in the same app and gave up. So I haven't tried it.

Finding a referenced document in all the existing collection using Mongoose

I have 3 collections :
events :
{
_id: ObjectId(54ca0f2506d0c6b673b2fde8),
_reference: ObjectId("54fd786c549e96f70f9c027d")
},
{
_id: ObjectId(54acd81941a646d768922cfa),
_reference: ObjectId("54fd786c549e96f70f9c027d")
}
posts :
{
_id: ObjectId(54fd786c549e96f70f9c027d),
title: "This is a post",
content: "This is the content of the post"
}
comments :
{
_id: ObjectId(54fd786c549e96f70f9c027e),
content: "This is a comment on a post"
}
When a post or a comment is created, a document is also created in the collection "events" with a property called "_reference". This "_reference" will hold the ObjectId of the comment or post.
Then i need to recover all the documents (saved in the other collections ; i.e posts and comments) thats referenced in each document in the collection "events".
i used the populate method but that only allows me to check in ONE collection when I need to check in ALL the existing collections.
Bellow is an example of how I defined the reference property in the mongoose model :
_reference: {type: Schema.Types.ObjectId, ref: 'posts'}
Thanks in advance !
You cannot define the schema of events like:
_reference: {type: Schema.Types.ObjectId, ref: 'posts'}
and expect it to refer to comments as well. Because you're telling explicitly that the reference is for posts.
A better approach would be to save an ObjectID in events for the object you want to save, add a field called type where you would say if it's a comment or a post, and according to that, look up the designated collection for the ObjectId.

Proper way to use .populate() mongodb Node.js

I am trying to attach the full user model to each comment in my comments section. Since mongodb doesn't have joins I have been trying to figure out how to use .populate() to add the user object from the controller. As I understand it, to use the populate function there are only two things you must do.
Define a ref in the model.
Call .populate() on the name of the field which has a ref defined.
I have defined the ref in the model to the 'User' model:
var commentSchema = new mongoose.Schema({
author: { type: String, ref: 'User' },
description: String,
commentID: String,
hasParent: String,
parentComment: String,
timestamp: { type: Number, default: 0 },
});
Then I am trying to add the user model to the author field in the comment object:
Comment
.find()
.populate('author')
.exec(function (err, comments) {
console.log(comments);
});
Is there a step that I am missing? I was hoping to see all the comments with the full user object in the author field of each comment.
The solution was deleting the comments collection from mongodb. Starting fresh it works as expected. If anyone runs into a similar problem try deleting the collection of objects that were created before you had added a ref to the model.
Please consider making following changes.
author: { type: String, ref: 'User' };
authot: { type: Schema.Types.ObjectId, ref:'User'};

Categories