I have a Loopback model of project.json
project.json
"properties" : {
"users": {
"type": [
{
"user_id": "string"
}
]
}
}
array of id stored string format
{
"users" : [
{
"user_id" : "5ae319e8ac5718155ca719d0"
},
{
"user_id" : "5ae31a4d770afb158ef6c048"
}
]}
how to store string as an objectId like this
{
"users" : [
{
"user_id" : ObjectId("5ae319e8ac5718155ca719d0")
},
{
"user_id" : ObjectId("5ae31a4d770afb158ef6c048")
}
]
}
In Project Model
"relations": {
"user": {
"type": "belongsTo",
"model": "user",
"foreignKey": "id"
}}
In User Model
"relations": {
"projects": {
"type": "hasMany",
"model": "project",
"foreignKey": "project_user_id"
}}
If there is no relations, You can use
const ObjectId = require('mongodb').ObjectId;
var userId=ObjectId("5ae319e8ac5718155ca719d0")
and create into db.
You can check the type using typeof(userId). It will be an object.