Skip to content

Instantly share code, notes, and snippets.

@merqlove
Last active February 11, 2017 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merqlove/981c93faa52b751c64a538c147a2e25d to your computer and use it in GitHub Desktop.
Save merqlove/981c93faa52b751c64a538c147a2e25d to your computer and use it in GitHub Desktop.
Empty map Ecto behavior
defmodule InFactApi.Article do
use Ecto.Schema
import Ecto.Changeset
alias InFactApi.{Repo}
@required_fields [:title, :text]
@optional_fields [:article_id, :categories, :tags, :authors, :url, :created_date, :raw, :state]
schema @collection_name do
field :title
field :article_id
field :created_date, Ecto.DateTime
field :text
field :state, :string, default: "normal"
field :url, {:array, :string}, default: []
field :categories, {:array, :string}, default: []
field :authors, {:array, :string}, default: []
field :tags, {:array, :string}, default: []
field :raw, :map, default: %{}
timestamps
end
def changeset(schema, params) do
cast(schema, params, @required_fields ++ @optional_fields)
end
end
[{
"_id" : ObjectId("577ee03644c6c310ceb59a1d"),
"article_id" : "3f5ff0c0ca06968af21e5855fb37d79d1bf08a26",
"authors" : [
"Some Name"
],
"categories" : [
"Category"
],
"inserted_at" : ISODate("2016-07-07T23:05:26.000Z"),
"raw" : {},
"tags" : [],
"text" : "Some text",
"title" : "Some name",
"updated_at" : ISODate("2017-02-10T18:28:07.000Z"),
"url" : [
"http://vesti.ru/"
],
"created_date" : ISODate("2016-04-25T20:45:09.000Z")
}]
iex(1)> InFactApi.Repo.get_by(InFactApi.Article, article_id: "3f5ff0c0ca06968af21e5855fb37d79d1bf08a26", raw: %{})
nil
[debug] QUERY OK db=0.6ms
FIND coll="articles" query=[{"$query", ["$and": [[article_id: "3f5ff0c0ca06968af21e5855fb37d79d1bf08a26"], [raw: []]]]}, {"$orderby", %{}}] projection=%{_id: true, article_id: t
rue, authors: true, categories: true, created_date: true, inserted_at: true, raw: true, state: true, tags: tr
ue, text: true, title: true, updated_at: true, url: true} []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment