Leo James
Leo James

Reputation: 21

How do i create a conditional query in mongoose

In laravel we create a conditional query and can implement later.

use App\Http\Models\User

$user = new User;
$query = $user->find("foo");
$result = $query->getPostByUser();

In Mongoose, i tried this way, but query reuturn a executed result.

import User from "../Models/User.js";

const query = await Post.find(
    {
        foo: "bar"
    }
);
const Post = await query.limit(5);

How can i create a conditonal query in mongoose like in laravel.

Upvotes: 0

Views: 273

Answers (1)

Kasi
Kasi

Reputation: 102

all mongoose model's methods will return query and then you need to execute your query with either await or .exec() so try this:


    const query = Post.find({foo: "bar"});
    const post = await query.limit(5);

Upvotes: 2

Related Questions