DB-ref in mongoose without Schema.ObjectId? - node.js

in my code people can follow other people.
So far everything is ok apart from this fact: in the userScheme I have this field.
, following: [{ type: Schema.ObjectId, ref: 'Users' }]
Since every user has an username, it's more versatile for me to use dbref with the username.
is there a way to do something like this?
, following: [{ type: Users.username, ref: 'Users' }]
Many thanks,
g

No, only ObjectId values that refer to the _id property of another collection can be used as refs.
Confirmed in the source code.

You can only reference via ObjectId to a collection like your first code snippet.
[{ type: Schema.ObjectId, ref: 'Users' }]
When making a query with populate(), you can specify the fields you want to return. In your case, it woud look something like
User.find({}).populate('following', 'username').exec(function(err,doc) {});
Each object in the following array would look like
{ _id: 'xxxxxxxxxx', username: 'xxxxxxxx' }

mongoose's populate function is mongodb's lookup in the abstract but populate only creates refs to object id but if you are sure then you will have a unique username for every user and then if you want to populate data using username then you can use lookup and result will be the same.

Related

How can I populate array of objects in mongoose?

My schema looks like:
const mock = new Schema({
category:...,
name:....,
doneBy: [{
type:mongoose.Schema.Types.ObjectId,
ref: 'User'
}]
})
Query: 'doneBy' property is an array of particular users who have done the task. When I console.log it prints only ids as "doneBy: [id1,id2,id3......]". Now I want to populate these ids to get the particular user's info. Please share the method.
You can populate it in your query like so:
mock.find({...}).populate('doneBy')
If you want to populate the users but only select certain fields (for example, the username):
mock.find({...}).populate({ path: 'doneBy', select: { username: 1 } }) // _id will also be returned by default

Mongoose search for an object's value in referenced property (subdocument)

I have two schemas:
var ShelfSchema = new Schema({
...
tags: [{
type: Schema.Types.ObjectId,
ref: 'Tag'
}]
});
var TagSchema = new Schema({
name: {
type: String,
unique: true,
required: true
}
});
I would like to search for all Shelves where the tags array has a tag with a specific value.
I have tried using:
modelShelf.find({tags 'tags.name': 'mytag'})...
but it does not work. It always returns an empty array.
Any idea?
Looking at db each Shelf instance links only the objectID of the tags.
I have used references because I need to work also with Tag(s) entities.
In mongoDB you essentially can't do this directly as queries target a single collection at a time. Recently there were added new features which allow some kind of join when using the aggregation framework but for your needs that is not necessary.
From your schemas I see that the tags' names are unique so you can first fetch your desired tag with something like
modelTag.find({name: 'mytag'})
in order to get the tag's ID and then query your shelf collection for this tag ID
modelShelf.find({tags: tagId})

How do I reference ObjectID in another collection in MongoDB & Node?

I'm fairly new to this, so bear with me, however I have 2 collections. One called photos and another called users.
In Node, I am taking the data and putting it into my MongoDB using mongoose. I've got this working fine with my Schema:
var picSchema = new Schema({
uid: String,
pid: String,
oFile: String
});
What I want to do though is for the uid, I want to add the ObjectId for the user uploading the photo. I can pass this as a String, however I thought that I would have had to have the field set as an ObjectId, but seems I cannot do this?
Unless I am missing something, I might as well just add the username in there and use that as a reference?
Use mongoose.Schema.Types.ObjectId to populate the field with an ObjectId. In this case, you would use User (or whatever the name of your User schema is).
var picSchema = new Schema({
uid: {type: mongoose.Schema.Types.ObjectId, ref: 'User'},
pid: String,
oFile: String
});
Further to this, you can also use the Mongoose method Populate if you wish to expand the User document within a Pic document. For example:
Pic.find({})
.populate('uid')
.exec(function(err, pic) {
console.log(pic);
// do something
});

Weird mongoose behavior when viewing ObjectIds?

When viewing a sub-document with Robomongo I see something like this:
"views" : [
ObjectId("53a478431275cf0f3d91e27d"),
ObjectId("53a478431275cf0f3d91e27d")
]
But when I pull down the object through Mongoose into node.js, I see something like this:
views:
[ { _bsontype: 'ObjectID',
id: 'T\u001aôj#Ü«m¢©Ö',
viewDate: '2015-07-07T23:21:32.259Z' } ]
Yes, the schema is a little different, and I'm trying to write a script to remediate the data into the new format.
The schema is currently
views: [{view:{type: Schema.Types.ObjectId, ref: 'users'},viewDate:{type: Date, default: Date.now}}],
But
A) Why does the view object look all messed up in the latter, and
B) How can I get what I see in Robomongo? (Answered. See edit)
EDIT: Question B is answered. If I do .lean() to my query, then I'll be able to get it back as a non-mongoose object and it'll look how I expect it to look. So that just leaves question A
I managed to reproduce this.
First, you declared a schema similar to this:
views : { type : Schema.Types.ObjectId, ref : 'users' }
You created and wrote documents to the database using that schema.
Then you changed the schema to your current:
views: [{
view : { type: Schema.Types.ObjectId, ref: 'users' },
viewDate : { type: Date, default: Date.now }
}]
Using that schema, you are reading the documents that you wrote to the database using the first schema.
Those schema are fundamentally different: the first is stored as a single ObjectId in the database (the term "subdocument" is a bit confusing, because in Mongoose, subdocuments are documents that are stored with their parent document; the method you're using is called "population" in Mongoose-speak), but the second schema makes views an array of documents that have two properties (view, which is stored as an ObjectId and viewData which is a date).
This confuses Mongoose because it tries to apply the second schema to documents that were written using the first schema, and because of that, it's showing the internal representation of an ObjectId object instead of a stringified version of it.
This also explains why .lean() shows the correct results, because that tells Mongoose to return raw documents (as they are stored in the database) instead of trying to convert them according to the schema.

Mongoose - Defining child's ObjectID in another Schema

There is a similar thread # Mongoose variable key name. However, he goes with another method instead of solving this. The other method is the OR part of my title.
EDIT - IGNORE THIS SECTION, I AM USING THE ALTERNATIVE NOW. Issue now lays with referencing a child's Object ID elsewhere.
I have the following array:
selections: [{
4433d18d31f3775756ac2a70: "542e91aa31f3775756abccda"},
{4433d18d31f3775756ac2a71: "542e916c31f3775756abccd8"},
{4433d18d31f3775756ac2a72: "542e934231f3775756abccdb"
}]
My schema is currently as follows:
selections: {
<something>: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Selection'
}
}
In place <something>, is there a way of essentially saying "I don't care what's here"?
ALTERNATIVELY, as this doesn't seem possible after scouring the internet, I can have the following schema:
selections: {
pid: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Competition.CHILD'
}
selection: {
type: mongoose.Schema.Types.ObjectId,
ref: 'Selection'
}
}
But the issue here is that ObjectID that is being used where <something> is a child schema inside of the schema Competition, and I can't find a way of saying that for the Object ID.
Any advice/help would be great please. Ideally I'd prefer the first solution but I understand it may not be possible. Thanks!
Use an array of objects with optional (but fixed) keys:
selections: [{
selection: {type: ObjectId, ref: 'Selection'},
child: {type: ObjectId, ref: 'Competition.CHILD'}
}]
This will enable you to do better queries, use mongoose population, etc.

Resources