1

I have this route for searching partial user input for orderId.

ordersAdminRouter.route('/searchorder/:term')
    .get(function(req, res){
        term = req.params.term;
        console.log(term);
        Orders.find({orderId: new RegExp(term)})
            .populate({ path: 'userPurchased products.product', select: '-username -password' })
            .exec(function(err, orders){
                if (err) throw err;
                console.log(orders);

                res.json(orders);
            });
    });

Here is my schema

var orderSchema = new Schema({
    orderId: { type: String },
    userPurchased: { type: Schema.Types.ObjectId, ref: 'users' },
    products: [
        {
            product: { type: Schema.Types.ObjectId, ref: 'products' },
            size: { type: String, required: true },
            quantity: { type: Number, required: true },
            subTotal: { type: Number, required: true }
        }
    ],
    totalQuantity: { type: Number },
    totalPrice: { type: Number },
    modeOfPayment: { type: String },
    shippingAd: { type: String },
    refNumber: { type: String },
    isDone: { type: Boolean, default: false },
    orderStatus: { type: String, default: 'Pending' },
    dateOrdered: { type: Date, default: Date.now() },
    fromNow: { type: String }
});

Now I need to search for the firstname and lastname inside userPurchased which I only get when I populate. How can search it?

Mix Austria
  • 925
  • 2
  • 15
  • 35
  • @ThomasBormans I think that method isn't that good performance wise. I need to get everything on the db then filter every 400ms which is my search input implementation. My order db is so large that it took sometimes 10secs because of base64 encoded images. Well if there is no other approach then I have no choice then. – Mix Austria Feb 14 '17 at 10:03
  • You are absolutely correct but the problem is the same (I think). That is why I flagged it as duplicate. – Thomas Bormans Feb 14 '17 at 10:06

1 Answers1

-1

I could not understand what kind of information you are saving in the 'userPurchased' field. I assume the users are in a different collection and you're only persisting the id of the user in the userPurchased. If you are just placing the id, you can search using the following query:

orderSchema.find({userPurchased : 'id'})

If you're putting an object with fields, there's no mystery either. Just use the . operator. For example:

orderSchema.find({userPurchased.firstName : 'name'})

Hope it helped you

Israel Zinc
  • 2,713
  • 2
  • 18
  • 30
  • your first statement is true. userPurchased is only an ID based on my given schema. I am searching for userPurchased.firstname but the fields will only exist after .populate method. That means I cant use orderSchema.find({userPurchased.firstname: 'name'}) because userPurchased is only an id BEFORE .populate – Mix Austria Feb 14 '17 at 09:31
  • I see, sorry if my answer was not what you was looking for. When you say you want to search for the first name, it means that you wanna perform a second search after the first find, am I right? – Israel Zinc Feb 14 '17 at 10:08
  • What I am trying to find is to make the search on the first statement i.e. on find() so that I can reduce the size of the returned documents. – Mix Austria Feb 14 '17 at 11:03