Primitives.org.ai

find

Find entities matching criteria

Query entities with SQL, Document, or Graph-style filters.

Import

import { DB } from 'ai-database'

const { db } = DB({
  Post: { title: 'string', tags: 'string[]', views: 'number' },
})

Syntax

db.Type.find(where)
db.Type.find(options)

Examples

// Simple equality
const published = await db.Post.find({
  status: 'published',
})

// Multiple conditions
const featured = await db.Post.find({
  status: 'published',
  featured: true,
})

Operators

OperatorDescriptionExample
$eqEqual (default){ status: 'active' }
$neNot equal{ status: { $ne: 'deleted' } }
$gtGreater than{ views: { $gt: 100 } }
$gteGreater or equal{ views: { $gte: 100 } }
$ltLess than{ price: { $lt: 50 } }
$lteLess or equal{ price: { $lte: 50 } }
$inIn array{ status: { $in: ['a', 'b'] } }
$ninNot in array{ status: { $nin: ['deleted'] } }
$containsArray contains{ tags: { $contains: 'ai' } }
$containsAllContains all{ tags: { $containsAll: ['a', 'b'] } }
$startsWithString prefix{ title: { $startsWith: 'How' } }
$endsWithString suffix{ email: { $endsWith: '@co.com' } }
$regexRegex match{ slug: { $regex: '^[a-z]+$' } }
$existsField exists{ avatar: { $exists: true } }

SQL-Style

const posts = await db.Post.find({
  views: { $gt: 100 },
  status: { $in: ['published', 'featured'] },
})

Document-Style

const posts = await db.Post.find({
  'metadata.wordCount': { $gte: 1000 },
  tags: { $containsAll: ['ai', 'tutorial'] },
})

Graph-Style

const posts = await db.Post.find({
  $rel: {
    type: 'authored',
    from: { type: 'User', where: { verified: true } },
  },
})

Logical Operators

// OR
const posts = await db.Post.find({
  $or: [
    { status: 'published' },
    { featured: true },
  ],
})

// AND
const posts = await db.Post.find({
  $and: [
    { views: { $gt: 100 } },
    { status: 'published' },
  ],
})

// NOT
const posts = await db.Post.find({
  $not: { status: 'deleted' },
})

With Options

const posts = await db.Post.find({
  where: { published: true },
  orderBy: 'createdAt',
  order: 'desc',
  limit: 10,
})
Was this page helpful?

On this page