Skip to content
This repository has been archived by the owner on Mar 6, 2023. It is now read-only.

managing multiple documents from db #9

Closed
MichaelSowah opened this issue Oct 12, 2018 · 8 comments
Closed

managing multiple documents from db #9

MichaelSowah opened this issue Oct 12, 2018 · 8 comments

Comments

@MichaelSowah
Copy link

Hi I Have two different documents in the db:

document 1

{
"userData" : {
"id":"16",
"phone_number":"+233247832697",
"first_name":"Michael",
"last_name":"Sowah",
"username":"mtsowah"
},
token: "9a647851935d03bbc87e25cef1bba0089efabcb1"
}

document 2

{
{
"id":"1",
"title":"Amazing! collection",
"entity":"Amazing! collection","location":"Accra, Ghana",
"description":"Amazingingly beautifull and quite affordable quality jeans..",
"category":"Fashion",
"type_id":"1",
"reaction":"praise",
"user_id":"2",
"fullname":"James Bond",
"username":"jamesbond007"
},
{
"id":"2",
"title":"collection",
"entity":"Amazing! collection","location":"Accra, Ghana",
"description":"Amazingingly beautifull and quite affordable quality jeans..",
"category":"Fashion",
"type_id":"1",
"reaction":"praise",
"user_id":"2",
"fullname":"James Bond",
"username":"jamesbond007"
}
}

  1. How do I get only document two from DB and how do I update and add more data/subdocuments to only document two when I want to?
  2. How do I delete/remove document one only?
@marioreggiori
Copy link
Owner

You have to Query them by _id. F.e. use the first({}) method to get the first document and then use the auto generated _id field to update/delete.
The second document isn't valid. Please check again. If you want to add an object to an array, this isn't possible at the moment.

@MichaelSowah
Copy link
Author

MichaelSowah commented Oct 27, 2018

Thanks for the response.
from this example https://github.com/netz-chat/flutter_examples/blob/master/objectdb/listview/lib/main.dart

is it possible to have say eg:
var data = [ { 'name': {'first': 'Alex', 'last': 'Boyle'}, 'message': 'abc', 'active': true }, { 'name': {'first': 'Maia', 'last': 'Herzog'}, 'message': 'def', 'active': true }, { 'name': {'first': 'Curtis', 'last': 'Smith'}, 'message': 'ghi', 'active': true }, { 'name': {'first': 'Jaquelin', 'last': 'Renner'}, 'message': 'jkl', 'active': false }, { 'name': {'first': 'Denis', 'last': 'Swift'}, 'message': 'mno', 'active': true }, { 'name': {'first': 'Anna', 'last': 'Metz'}, 'message': 'pqr', 'active': true }, { 'name': {'first': 'Malinda', 'last': 'Reynolds'}, 'message': 'stu', 'active': false }, ];
and then in
db.insertMany(data);
is this possible? or how do I achieve something like this. Because my data coming from my server is formated something like this
{ 'name': {'first': 'Alex', 'last': 'Boyle'}, 'message': 'abc', 'active': true }, { 'name': {'first': 'Maia', 'last': 'Herzog'}, 'message': 'def', 'active': true },

and I want to do db.insert with

@marioreggiori
Copy link
Owner

marioreggiori commented Oct 27, 2018

You have to strip the last comma, wrap the response in json array brackets and replace single quotations marks with double quotations marks f.e.

[
	{"name": {"first": "Alex", "last": "Boyle"}, "message": "abc", "active": true },
	{ "name": {"first": "Maia", "last": "Herzog"}, "message": "def", "active": true }
]

Now you can jsonDecode the string and insert the data with db.insertMany(data)

@MichaelSowah
Copy link
Author

MichaelSowah commented Oct 27, 2018

I get this error when I tried.
List<dynamic>' is not a subtype of type 'List<Map<dynamic, dynamic>>

@marioreggiori
Copy link
Owner

Could you please post the "print(data)" of the object you try to insert?

@MichaelSowah
Copy link
Author

This is the decoded data from HTTP response.body, then I try to insert into db

[{id: 12, title: , entity: Pizza Inn, location: Achimota, description: Chicken is awesome., category: none, type_id: 1, reaction: null, user_id: 16, blob_id: 16, status: active, created_date: 2018-09-07 12:46:47, fullname: Michael, username: mtsowah, yaip_blob_url: 1536346007_b64conv.jpg, yaip_type: Image, profile_picture_id: 5, profile_picture: 1535715086_b64conv.jpg, praise_count: 1, shame_count: 0, comments_count: 0, category_list: 'none'}, {id: 9, title: , entity: Mothership LA | Serge Attukwei Clottey, location: La, Accra, description: Using his body as an object Ghanaian artist Serge Attukwei Clottey works with international media - as well as plastic yellow "gallons" (jerrycans) - to speak about politics, religion, sex and tradition., category: none, type_id: 1, reaction: shame, user_id: 16, blob_id: 13, status: active, created_date: 2018-09-06 09:02:40, fullname: Michael, username: mtsowah, yaip_blob_url: 1536246160_b64conv.jpg, yaip_type: Image, profile_picture_id: 5, profile_picture: 1535715086_b64.jpg, praise_count: 1, shame_count: 0, comments_count: 0, category_list: 'none'}]

@marioreggiori
Copy link
Owner

Try something like this:

var data = List<Map<dynamic, dynamic>>.from(jsonDecode(jsonData));
db.insertMany(data);

@MichaelSowah
Copy link
Author

Try something like this:

var data = List<Map<dynamic, dynamic>>.from(jsonDecode(jsonData));
db.insertMany(data);

this worked, thanks, you are awesome

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants