Ottoman v2 schema type

Hi,

How to create a schema nested address object where the field name is a type?

const user = new Schema({
  id: String,
  name: String,
  address: {
    location: String,
    type: { type: String },
  }
});

I tried the above schema but result: Error: Property address is a required type.

Also to add on, using https://v2.ottomanjs.com/guides/schema.html#allowed-schematypes-are.

Are there any example of Embed and Reference schema type?

The Reference link is broken btw.

Below code should solve your purpose
// Declare Address Schema
const addressSchema = new Schema({
location: String,
type: String
})
//create a model for Address
const Address = model(‘Address’, addressSchema)

const userSchema = new Schema({
id: String,
name: String,
address: { type: String, ref: ‘Address’ }
})
const User = model(‘User’, userSchema)
// create an address
const address1 = new Address({
location : “somelocation”,
type : “residential”
})

const createAndSaveAddress = async() => {
try {
await address1.save()
console.log(success: Address1 added )
} catch (error) {
throw error
}
}
const createAndSaveUser = async() => {
try {
const user1 = new User({
id : “id123”,
name : “John Doe”,
address: address1
})

await user1.save()

console.log('success: User1 added.')

} catch (error) {
throw error
}
}

You can use populate, depopulate and populated types of methods

Here is the link to the documentation for example. https://v2.ottomanjs.com/guides/document.html#populate

Dont have an example for Embed

The Reference link being broken is an known issue and we are working on getting that fixed :slight_smile:

1 Like