References in MongoDB / Mongoose / nodejs - parallelization - javascript

I want to get references in mongoDB using nodejs/mongoose.
In the documentation I read that there are two options: Manual References or DBRefs.
Since they state, its recommended to use Manual References, I decided to set up a schema in the following way:
var schema = new mongoose.Schema({
name : String,
reference : mongoose.Schema.ObjectId
});
Question: If I retrieve an array of these objects from my collection, how do I resolve the references in a good practice way?
My Idea was to use Nimble and parallelize the necessary requests. I wanted to do something like
flow.parallel(functions, function() {
return result;
});
where I dynamically fill an array of functions
var functions = []
which I pass then to nimble.
(kind of this SO-question: Javascript Array of Functions)
Question: Is this practical? The array of functions-thing seems kind of not really the way to go to me. But I don't see any alternative, since nimble needs to be called with a static number of functions.

You can use Mongoose's support for reference population to efficiently follow references.
var schema = new mongoose.Schema({
name : String,
reference : { type: mongoose.Schema.ObjectId, ref: 'OtherModel' }
});
var MyModel = mongoose.model('MyModel', schema);
MyModel.find().populate('reference').exec(function(err, docs) {...});
In the above example, the reference field of each docs element gets populated with referenced doc.

Related

Document.get on nested document returns undefined

When I use Document.get on a nested document it returns undefined, where as when I use Document.get with the full path it works
Example:
const PostsSchema = new mongoose.Schema({
author: {
name: String
}
});
const Posts = mongoose.model('posts', PostsSchema);
const doc = new Posts({
author: {
name: 'Harry'
}
});
console.log(doc.get('author.name'));
// "Harry"
console.log(doc.author.get('name'));
// undefined
I believe the difference between the two ways you've presented is related to the difference between Subdocuments and nested paths as explained in the official doc: Subdocuments versus Nested Paths
Comparing to the examples given, 'author' is not a Subdocuments of the 'Posts', it's a property defined in the Schema, therefore, the correct syntax, as presented in the official doc Document.prototype.get(), is to pass the path as a string (as the first way you typed):
doc.get(path,[type],[options])
path «String»
[type] «Schema|String|Number|Buffer|*» optionally specify a type for on-the-fly attributes
[options] «Object»
[options.virtuals=false] «Boolean» Apply virtuals before getting this path
[options.getters=true] «Boolean» If false, skip applying getters and just get the raw value

Problem in handling many field through function in mongoose

I want to generate field in mongoose with function.
Because there are many fields, but they are much the same, I wanna use function to create them to keep code short.
I wrote a function, but there exists lints.
import { Schema } from 'mongoose'
function fieldGen(name, type="string", isRequired=true) {
var field = {}
field[name] = {
type: type,
required: isRequired
}
return {...field}
}
const testSchema = new Schema({
fieldGen("firstname")
fieldGen("lastname")
fieldGen("location")
})
In VS Code, Problem shows as below
Identifier expected. ts(1003) [20, 12]
I expect first argument "firstname" matches name in function, and return object.
You're adding values to your testSchema object without giving them names.
Also, you're spreading the properties of the field object into a new object literal. That doesn't accomplish anything. Just returning the field object would yield the same result.
I see what you are trying to do. If you debug this in smaller steps and take a closer look at the data you are handling, I think you'll figure it out on your own.

Is there an equivalent to Dictionary or similar collection in Dojo?

I'm looking for a way to store objects in a collection and identify them by UUID, then query the collection to get an object by UUID. The closest example I can think of is the Dictionary collection in .NET. Is there a recommended functionality in Dojo for this?
There is no equivalent in dojo, and even when the dojo/store/Memory module is not meant for this purpose, you can use it in some way as a collection, check this out:
require(["dojo/store/Memory"], function(Memory){
var someData = [
{id:1, name:"One"},
{id:2, name:"Two"}
];
store = new Memory({
idProperty: "id", //define the id property
data: someData
});
// Returns the object with an id of 1
store.get(1);
// Returns query results from the array that match the given query
store.query({name:"One"});
// Pass a function to do more complex querying
store.query(function(object){
return object.id > 1;
});
// Returns query results and sort by id
store.query({name:"One"}, {sort: [{attribute: "id"}]});
// store the object with the given identity
store.put({id:3, name:"Three"});
// delete the object
store.remove(3);
});
dojo has other types of store that may suit more to your case than the dojo/store/Memory. Here are some links to docs:
dojo/store/Memory
dojo/store/JsonRest
dojo/store/DataStore
dojo/store/Cache
Exist others, but this are the must commons
There is not such objects has Collection in JavaScript. But you could create your own Objects like below
var coll = { uid : "value" };
coll['uid2'] = "value2;
This can be accessed using
var a = coll['uid'];
This is the beauty of JavaScript.

Cannot add a key to an object in Javascript [duplicate]

I have a document from a mongoose find that I want to extend before JSON encoding and sending out as a response. If I try adding properties to the doc it is ignored. The properties don't appear in Object.getOwnPropertyNames(doc) making a normal extend not possible. The strange thing is that JSON.parse(JSON.encode(doc)) works and returns an object with all of the correct properties. Is there a better way to do this?
Mongoose Models inherit from Documents, which have a toObject() method. I believe what you're looking for should be the result of doc.toObject().
http://mongoosejs.com/docs/api.html#document_Document-toObject
Another way to do this is to tell Mongoose that all you need is a plain JavaScript version of the returned doc by using lean() in the query chain. That way Mongoose skips the step of creating the full model instance and you directly get a doc you can modify:
MyModel.findOne().lean().exec(function(err, doc) {
doc.addedProperty = 'foobar';
res.json(doc);
});
JohnnyHK suggestion:
In some cases as #JohnnyHK suggested, you would want to get the Object as a Plain Javascript.
as described in this Mongoose Documentation there is another alternative to query the data directly as object:
const docs = await Model.find().lean();
Conditionally return Plain Object:
In addition if someone might want to conditionally turn to an object,it is also possible as an option argument, see find() docs at the third parameter:
const toObject = true;
const docs = await Model.find({},null,{lean:toObject});
its available on the functions: find(), findOne(), findById(), findOneAndUpdate(), and findByIdAndUpdate().
NOTE:
it is also worth mentioning that the _id attribute isn't a string object as if you would do JSON.parse(JSON.stringify(object)) but a ObjectId from mongoose types, so when comparing it to strings cast it to string before: String(object._id) === otherStringId
the fast way if the property is not in the model :
document.set( key,value, { strict: false });
A better way of tackling an issue like this is using doc.toObject() like this
doc.toObject({ getters: true })
other options include:
getters: apply all getters (path and virtual getters)
virtuals: apply virtual getters (can override getters option)
minimize: remove empty objects (defaults to true)
transform: a transform function to apply to the resulting document before returning
depopulate: depopulate any populated paths, replacing them with their original refs (defaults to false)
versionKey: whether to include the version key (defaults to true)
so for example you can say
Model.findOne().exec((err, doc) => {
if (!err) {
doc.toObject({ getters: true })
console.log('doc _id:', doc._id)
}
})
and now it will work.
For reference, see: http://mongoosejs.com/docs/api.html#document_Document-toObject
To get plain object from Mongoose document, I used _doc property as follows
mongooseDoc._doc //returns plain json object
I tried with toObject but it gave me functions,arguments and all other things which i don't need.
The lean option tells Mongoose to skip hydrating the result documents. This makes queries faster and less memory intensive, but the result documents are plain old JavaScript objects (POJOs), not Mongoose documents.
const leanDoc = await MyModel.findOne().lean();
not necessary to use JSON.parse() method
You can also stringify the object and then again parse to make the normal object.
For example like:-
const obj = JSON.parse(JSON.stringify(mongoObj))
I have been using the toObject method on my document without success.
I needed to add the flattenMap property to true to finally have a POJO.
const data = document.data.toObject({ flattenMaps: true });

Is there a native feature to convert string based JSON into Mongoose Schema object instance?

I am using Express and I am looking for a convenient way to convert this kind of object (which comes on the request req.body.myObject):
{
"name": "Foo",
"someNumber": "23",
"someBoolean": "on"
}
Into an instance of this Schema:
var myObjectSchema = new Schema({
name: String,
someNumber: Number,
someBoolean: Boolean
});
Notice that the first object comes from the request, so its made entirely by Strings.
Is there some nice way to achieve this? If not, would you have any suggestions on how to implement this feature as a middleware???
By referring to this thread Mongoose : Inserting JS object directly into db I figured out that yes, theres a built in feature for this.
You simply build a new model passing request values (from the form) as parameters:
function add(req, res){
new Contact(req.body.contact).save(function(err){
console.log("Item added");
res.send();
});
};
It automatically converts stuff for you!
I know this answer has already been accepted, but I wanted to point out that mongoose takes care of most of the casting for you... most of the time. While it's convenient that mongoose does this, it abstracts away the true behavior of mongo. For example, mongoose lets you do something like this:
PersonModel.findById("4cdf00000000000000007822", ...);
However, if you tried to query the database directly (without mongoose), this would not work:
PersonCollection.find({_id: "4cdf00000000000000007822"}, ...);
This is because ObjectIds are not strings... they are objects. Internally, mongoose converts that string to an ObjectId and then performs a query against the database so that the final query looks kinda like this:
PersonCollection.find({_id: ObjectId("4cdf00000000000000007822")}, ...);
Also, each path in a schema has a "caster" method. This is a private method, but it's darn handy when you need it. PLEASE NOTE THAT THE caster METHODS DESCRIBED BELOW ARE UNDOCUMENTED AND CAN CHANGE WITHOUT WARNING. USE AT YOUR OWN RISK (sorry for yelling):
// Use PersonModel.schema.paths() to get all paths and loop over them if you want
var key = "name";
var pathObj = PersonModel.schema.path( key );
if( !pathObj ) pathObj = PersonModel.schema.virtualpath( key );
if( !pathObj ) { /* not found: return, continue, exit, whatever */ }
// UNDOCUMENTED: USE AT YOUR OWN RISK
var caster = pathObj.caster || pathObj;
var castedValue = caster.cast( req.body.name );
Why do I know this? Because if you want to use some of the more advanced features of mongo such as aggregation, you will need to cast your own values as you build the pipeline. I have also needed to manually cast values for certain queries which used the $in operator... maybe this is not needed any more. Point is, if you are having trouble getting the results you expect, try casting the values yourself.
Provided the schema is static, one could theoretically go the lazy, non-sophisticated way and just hardcode the values instead of passing the object itself:
var someObject = {
name: "Foo",
someNumber: "23",
someBoolean: "on"
}
var myObjectSchema = new Schema({
name: someObject.name,
someNumber: parseInt(someObject.someNumber, 10),
someBoolean: (someObject.someBoolean == "on")
});
Possibly not the answer you were looking for, but might be something to consider if nothing better is available.

Categories

Resources