piyushcoader

piyushcoader

Hi is there any way to connect mongodb with Ecto with latest version? I really need mongodb as a datastore. thanks in advance
I have gone through couple but this is not working well with latest release

  1. GitHub - elixir-mongo/mongodb_ecto: MongoDB adapter for Ecto · GitHub

  2. GitHub - elixir-mongo/mongodb: MongoDB driver for Elixir · GitHub

if anyone is using mongo in production with phoenix please share your experience
Thanks in Advance

Showing Posts 13 to 4

Morganjackson

Morganjackson

Sorry so keep bringing this up, but I’m still not sure I’m using this correctly.

I have an application with 20+ templates called blocks which can all potentially have different styles. They all share a “Block” schema but need a flexible way to add different styles which is why I thought jsonb would be the way to go.

[%Ap.Block{__meta__: #Ecto.Schema.Metadata<:loaded, "blocks">, 
  id: 1, 
  name: "foo",
  style: [%Ap.CustomField{
    data: [
      %{"color" => "red"},
      %{"h2" => "#303030"},
      %{"background-image" => "image.jpg"}
    ],
    id: "4222329c-cac4-4e1d-bfa6-6c440dcb7b59"}
  ], 
  template: "baz",
  inserted_at: #Ecto.DateTime<2016-11-18 00:58:41>,
  updated_at: #Ecto.DateTime<2016-11-18 00:58:41>}]

Basically, I want my final template to look something like the pseudo code below but I’m still not sure the best way to save the data and then how to easily access it later?

<style scoped>
  p {
    color: <%= style.data.color %>
  }
  h2 {
    color: <%= style.data.h2 %>
  }
</style>

<section style="background-image: <%= style.data.background-image %>">
  <h2>My Heading</h2>
  <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor.</p>
</section>
OvermindDL1

OvermindDL1

hd/1 and tl/1 are from erlang, and thus in the Elixir Kernel module. :slight_smile:

Morganjackson

Morganjackson

Thank you! that worked perfectly.

Looks like I need to read up on lists as I wasn’t aware of hd()

ericmj

ericmj

Elixir Core Team

That does look correct. These are normal elixir values so you can access color by hd(hd(block.style).data)["color"].

Morganjackson

Morganjackson

@ericmj @hubertlepicki thanks for replying guys.

I think my data is either in the wrong format or I’m missing something.

Does the result below look correct? if so, how do I access “color”?

[%Ap.Block{__meta__: #Ecto.Schema.Metadata<:loaded, "blocks">, 
  id: 1, 
  name: "foo",
  style: [%Ap.CustomField{
    data: [%{"color" => "red"}],
    id: "4222329c-cac4-4e1d-bfa6-6c440dcb7b59"}
  ], 
  template: "baz",
  inserted_at: #Ecto.DateTime<2016-11-18 00:58:41>,
  updated_at: #Ecto.DateTime<2016-11-18 00:58:41>}]
ericmj

ericmj

Elixir Core Team

Ecto translates between jsonb and elixir maps so you will be working with normal elixir maps in your templates.

hubertlepicki

hubertlepicki

I am sorry, I don’t know. I only used it with :map data type. I do not try to use too many ORM features as my relationship with those libs generally “is complicated”.

When you use :map it uses Elixir’s maps that are pretty natural translation from one to another.

Morganjackson

Morganjackson

@hubertlepicki Do you happen to know of any good examples of using JSONB?

I’m trying to migrate a Meteor application to phoenix, but there are a couple of areas that use custom fields and I’m struggling to find examples on how to handle them.

So far I have this:

embeds_many :style, App.CustomField

and then

defmodule App.CustomField do 
    embedded_schema do
      field :data, {:array, :map}
    end
end

This saves the JSONB correctly, but I’m not sure how to display it in my eex templates? Do I need to use Poison?

hubertlepicki

hubertlepicki

yes, it can, and if you go with UUID for IDs and JSONB as datatype you get more or less the same “feel” as with Mongo with full support from Phoenix. JSONB field just becomes Map in Elixir.

piyushcoader

piyushcoader OP

Thanks, I am pretty new to postgres coming from meteor background. was using mongo alot but just found postgress can store jsonp data. i think i will stick with postgress now. thanks

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New

Latest on Elixir Forum

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews