sylv1un
sylv1un

Reputation: 21

I can't save same value on array in mongodb

I have a problem for saving (same) values in array in mongodb:

Here my schema what I use to create a user :

    const userSchema = new mongoose.Schema(
  {
   
    email: { type: String, required: true },
    addr: { type: String, required: true, unique: true },
    isFirstConnection: { type: Boolean, default: true, required: true },
    type:{ type: String },
    firstName:{ type: String },
    lastName:{ type: String },
    companyName:{ type: String },
    birthDate:{ type: String },
    nationality:{ type: String },
    description:{ type: String },
    avatar:{ type: String },
    isAdmin: { type: Boolean, default: false, required: true },
    isSeller: { type: Boolean, default: false, required: true },
    seller: {
      name: String,
      banner: String,
      description: String,
      rating: { type: Number, default: 0, required: true },
      numReviews: { type: Number, default: 0, required: true },
      collection: {type: Array}

      
      //collection: [{type: String, required:true ,unique: true, default:'My Default Collection'}]
     
    },
    
  },
  {
    timestamps: true,
  }
);
const User = mongoose.model('User', userSchema);
export default User;

and here my code to create a new user :

userRouter.post(
  '/signin',
  expressAsyncHandler(async (req, res) => {
    
    
    const user = await User.findOne({ addr: req.body.addr });
    if (user) {
        res.send({
          _id: user._id,
          addr: user.addr,
          email: user.email,
          isAdmin: user.isAdmin,
          isSeller: user.isSeller,
          isFirstConnection: user.isFirstConnection,
          token: generateToken(user),
        }); 
        return;

    } else {

    const newUser = new User({
      email: req.body.email,
      addr: req.body.addr,

      seller:{
       
        collection:["Default Collection"],

      },
    });
    
    
    const createdUser = await newUser.save();
    res.send({
      _id: createdUser._id,
      email: createdUser.email,
      isAdmin: createdUser.isAdmin,
      isSeller: createdUser.isSeller,
      isFirstConnection: createdUser.isFirstConnection,
      token: generateToken(createdUser),
    });
    
  }
  })

);

The actual state :

If I don't have user in my database it's ok the user is created. But after creating a user in database (mongodb), I can't create another user. After researching the problem I know that this one comes from my userSchema at this line :

collection: {type: Array}

but I don't know why ? I found that creating a user failed if I tried to save a user with a value in the "collection" array that was the same as the value in that same array for another user. But really I don't understand why.

So my question is :

how to save identical values ​​in my "collection" array and also between users ?

Thanks for your help, I'm completely lost....

Upvotes: 0

Views: 177

Answers (1)

sylv1un
sylv1un

Reputation: 21

Ok sorry I just found after 3 days of research, and it was really stupid, but you had to know.

In mongodb's "Indexes" settings my "collection" had a "unique" property. I deleted it and everything is ok

Upvotes: 1

Related Questions