You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to load a JSON file from an external server and replace the one that already exists, because I mainly want to use sleekDB's search & filter. Is there the possibility to replace a file in sleekDB?
I tried that, but a new file with a new _id is always created?
The first thought is, that you need to delete all the old users.
Perhaps I get you wrong. As long, you won't provide an _id, sleek will create new users. For updating, the _id is required. A workaround could be this:
$databaseDirectory = $_SERVER['DOCUMENT_ROOT'] . "/data/test";
$storeConfiguration = [
"auto_cache" => true,
'timeout' => false,
];
$userStore = new \SleekDB\Store("customers", $databaseDirectory, $storeConfiguration);
$newUsers = [
[
"username" => "Lisa",
"age" => 17,
"address" => [
"street" => "up street",
"streetNumber" => 48,
"postalCode" => "1822",
],
],
[
"username" => "Bob",
"age" => 20,
"address" => [
"street" => "down street",
"streetNumber" => 12,
"postalCode" => "8174",
],
],
];
$oldUsers = $userStore->findAll();
$updateOrInsertData = [];
foreach ($newUsersas$user) {
// Check if there is already a user with that usernameif ($oUser = $userStore->findOneBy(['username', '=', $user['username']])) {
// Already inside database, add _id to the new element$updateOrInsertData[] = array_merge($user, ['_id' => $oUser['_id']]);
} else {
// Not inside database, just add it$updateOrInsertData[] = $user;
}
}
// Update all existing documents with the new data$userStore->updateOrInsert($newUsers);
I would like to load a JSON file from an external server and replace the one that already exists, because I mainly want to use sleekDB's search & filter. Is there the possibility to replace a file in sleekDB?
I tried that, but a new file with a new _id is always created?
The text was updated successfully, but these errors were encountered: